Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error show pdf CrystalReport vb.net

I have the code below its function is to load the data from a report on the screen using the CrystalReports.

Dim strExportFile As String
            strExportFile = "ReportReajustesAplicados.pdf"

            Dim s As System.IO.MemoryStream = relat.ExportToStream(ExportFormatType.PortableDocFormat)
            With HttpContext.Current.Response

                .ClearContent()
                .ClearHeaders()
                .ContentType = "application/pdf"
                .AddHeader("Content-Disposition", "inline; filename=" & strExportFile)
                .BinaryWrite(s.ToArray)
                .End()
            End With

When I do the extraction of the data.

I have the following error

Unable to cast object of type 'FileStreamDeleteOnClose' to type 'System.IO.MemoryStream'.

I tried using System.IO.Stream, extraction works but does not display the data on the screen because ".BinaryWrite (s.ToArray)" does not accept the method ToArray.

Note: When I put

#if DEBUG Then
             CrystalReportViewer1.ReportSource = relat
             CrystalReportViewer1.DataBind ()
             Exit Sub

If #End

works

I need this to work but in Release mode.

Help me

like image 325
Thamires Cunha Avatar asked Jul 24 '15 20:07

Thamires Cunha


2 Answers

I found the solution on:

https://archive.sap.com/discussions/thread/3322762

As answered by SAP guy: "This is by design, we never fully supported export to MemoryStream. Only option is to not use MemoryStream, this will not be changed."

So the solution is to replace MemoryStream with Stream and send it in Byte array as in:

Dim strExportFile As String
strExportFile = "ReportReajustesAplicados.pdf"
Dim s As System.IO.Stream = relat.ExportToStream(ExportFormatType.PortableDocFormat)
Dim b(s.Length) As Byte
s.Read(b, 0, CInt(s.Length))

With HttpContext.Current.Response
    .ClearContent()
    .ClearHeaders()
    .ContentType = "application/pdf"
    .AddHeader("Content-Disposition", "inline; filename=" & strExportFile)
    .BinaryWrite(b)
    .Flush()
    .SuppressContent = True
    HttpContext.Current.ApplicationInstance.CompleteRequest()
End With
like image 183
Matt Roy Avatar answered Nov 03 '22 23:11

Matt Roy


try to use this code to convert in stream:

//MemoryStream oStream = (MemoryStream)reportDocument.ExportToStream(type);
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = reportDocument.ExportToStream(type);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));

here is the full code as reference:

private void ShowReports(ReportDocument reportDocument, string fileName)
        {
            string rptType = GetQsSValue("ReportType");
            ExportFormatType type = ExportFormatType.PortableDocFormat;
            string rptTypeExt = string.Empty;
            string contentType = string.Empty;

            if (rptType == null || rptType == "PDF")
            {
                type = ExportFormatType.PortableDocFormat;
                rptTypeExt = ".pdf";
                contentType = "application/pdf";
            }
            else if (rptType == "Excel" || rptType == "Text")
            {
                type = ExportFormatType.Excel;
                rptTypeExt = ".xls";
                contentType = "application/excel";               
            }

            //MemoryStream oStream = (MemoryStream)reportDocument.ExportToStream(type);
            System.IO.Stream oStream = null;
            byte[] byteArray = null;
            oStream = reportDocument.ExportToStream(type);
            byteArray = new byte[oStream.Length];
            oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));

            reportDocument.Close();
            reportDocument.Dispose();
            reportDocument = null;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = contentType;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + rptTypeExt);
            //HttpContext.Current.Response.BinaryWrite(oStream.ToArray());
            HttpContext.Current.Response.BinaryWrite(byteArray);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
            GC.Collect();
        }
like image 45
Haseeb Ahmed Avatar answered Nov 03 '22 23:11

Haseeb Ahmed