VB to C# conversion help

R

Russ

I am trying to convert this VB code to C#. Can someone give me a hand. The
code is for exporting a crystal report to a PDF using a memory stream.

Here is the VB:
Dim s As System.IO.MemoryStream =
oRpt.ExportToStream(ExportFormatType.PortableDocFormat)
' the code below will create pdfs in memory and stream them to the
browser
' instead of creating files on disk.
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("Content-Disposition", "inline; filename=Report.pdf")
.BinaryWrite(s.ToArray)
.End()
End With

Here is what I thought would be the C# translation:

System.IO.MemoryStream mystream = new System.IO.MemoryStream();
mystream = oRpt.ExportToStream(ExportFormatType.PortableDocFormat);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;
filename=Report.pdf");
HttpContext.Current.Response.BinaryWrite(mystream.ToArray());
HttpContext.Current.Response.End();

The compiler gives a error on the second line of this code stating 'cannot
convert from System.IO.stream to System,IO.MemoryStream

Thanks for the help,
Russ
 
H

Hermit Dave

Try
System.IO.MemoryStream mystream =
oRpt.ExportToStream(ExportFormatType.PortableDocFormat);

you were trying to create a new value when oRpt.ExportToStream already
returns a stream....
you can create a derived class and just assign it to base class instance ...
but you cannot assign instance unless it has overloaded equals operator that
takes base class instance...

HTH
 
H

Hermit Dave

you might also want to try an explicit typecast

System.IO.MemoryStream mystream = (System.IO.MemoryStream mystream)
oRpt.ExportToStream(ExportFormatType.PortableDocFormat);
 
H

Hermit Dave

sorry not trying to confuse you .... just being lame... i guess i need to go
out and freshen up...

System.IO.MemoryStream mystream = (System.IO.MemoryStream)
oRpt.ExportToStream(ExportFormatType.PortableDocFormat);

--
Regards,

HD

PS: by mistake i had just copied mystream in typecast as well...
 
R

Russ

Thanks for the response. I actually tried this one before I checked back:

System.IO.MemoryStream mystream = (System.IO.MemoryStream mystream)
oRpt.ExportToStream(ExportFormatType.PortableDocFormat);

and it was the answer to the problem.

Thanks,
Russ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top