Thamires Cunha
Thamires Cunha

Reputation: 85

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

Upvotes: 7

Views: 11457

Answers (5)

Alan Shaw
Alan Shaw

Reputation: 61

For those who are not working with web servers and just need to save it:

Dim myStream As New System.IO.MemoryStream

'ExportToStream acutally produces type: FileStreamDeleteOnClose -- not Stream.  Can't implicitly convert it to MemoryStream   
Dim myTempStream As System.IO.Stream
myTempStream = oRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)

'Now copy the data to a MemoryStream before closing the report and destroying the stream.
myTempStream.CopyTo(myStream)

'Clean up and close the temp stream and report.
oRpt.Close()

Upvotes: 0

Haseeb Ahmed
Haseeb Ahmed

Reputation: 243

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();
        }

Upvotes: 4

RHelm
RHelm

Reputation: 43

I was having a similar problem when running locally. Instead of using a stream, use this:

using (CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt =
    new ReportDocument())    
{    
    oRpt.Load(Server.MapPath("../RptFiles/MyCReport.rpt"));   
    oRpt.SetDataSource(MyDataSource);  

    // ... other stuff you need, params, etc  

    Response.Clear();                  
    Response.ClearHeaders();

    oRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false,
        "MyReportName");

    oRpt.Close();    
}

Upvotes: 1

Matt Roy
Matt Roy

Reputation: 1535

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

Upvotes: 5

David Gourde
David Gourde

Reputation: 3914

You could replace .BinaryWrite(s.ToArray) by .BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray()).

I wrote an example below, I'm not sure if this is what you're looking for, but I hope it can help you. This works for me to let users download/view Crystal Reports using the PDF format.

 Dim fileName As String = "ReportReajustesAplicados.pdf"
 Response.ContentType = "application/pdf"
 Response.Clear()
 Response.Buffer = True
 Response.AddHeader("Content-Disposition", "filename=" + fileName)
 Response.BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray())
 Response.End()

I put the Crystal report in the session with those options (rpt is my Crystal Report):

Dim options = rpt.ExportOptions
options.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
Session!myCrystalReport= rpt.FormatEngine.ExportToStream(New CrystalDecisions.Shared.ExportRequestContext With {
        .ExportInfo = options
        })
rpt.Dispose()

If my answer is unclear or is not what you were looking for, please tell me and I will try to update it as soon as I can.

Upvotes: 0

Related Questions