VB to C# conversion help

  • Thread starter Thread starter Russ
  • Start date Start date
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
 
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
 
you might also want to try an explicit typecast

System.IO.MemoryStream mystream = (System.IO.MemoryStream mystream)
oRpt.ExportToStream(ExportFormatType.PortableDocFormat);
 
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...
 
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
 
Back
Top