cancel
Showing results for 
Search instead for 
Did you mean: 

CrystalReports 'System.InvalidCastException' occurred

Former Member
0 Kudos

Hi Team, I'm getting the below error. Can some one please assist me on this? I'm using Windows 2012 R2 server and CrystalReport Runtime 13.0.x version installed on the server. Application built on Any CPU using VisualStudio 2010 Error Message: HandlingInstanceID: c68a0e37-5eb5-4cb5-9587-6d6030b79b48  An exception of type 'System.InvalidCastException' occurred and was caught.  ---------------------------------------------------------------------------  01/13/2015 16:26:11  Type : System.InvalidCastException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089  Message : Unable to cast object of type 'FileStreamDeleteOnClose' to type 'System.IO.MemoryStream'.

Accepted Solutions (1)

Accepted Solutions (1)

former_member183750
Active Contributor
0 Kudos

Hi Singareddy

Please provide the following information:

Service pack used with your SAP Crystal Reports, Developer Version for Visual Studio .NET

Code, or steps as to how to reproduce the issue

Is this happening on your development computer or after you deploy the app?

- Ludek

Senior Support Engineer AGS Product Support, Global Support Center Canada

Follow us on Twitter

Answers (1)

Answers (1)

0 Kudos

It appears you are using System.IO.MemoryStream in your export method

Don't, not supported. Use System.IO.Stream.

See this KBA

SAP Note 2105311 - ExportToStream throws exception in SP 12 in Crystal Reports for Visual Studio

Don

Former Member
0 Kudos

I have been reading a lot of posts about this issue.  I have also ready the KBA at the link provided, but it does not exactly state the issue that I am having, but it is very close.

I too am receiving the error:  Unable to cast object of type 'FileStreamDeleteOnClose' to type 'System.IO.MemoryStream'.  However, that is due to code that I have been using for several years where I was turning the crystal reports returned IO.Stream and then casting it to an IO.MemoryStream.

The problem now is not that I am trying to get an IO.MemoryStream out of the ExportToStream function, it is that I used to get an IO.Stream as a result from the function and now I am getting back a FileStreamDeleteOnClose instead.  This type, FileStreamDeleteOnClose then is not able to be cast to an IO.MemoryStream item.

The ExportToStream function is supposed to return an IO.Stream item and it no longer is, which is causing the whole issue.  In the KBA article listed above states this:

  • CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream only returns System.IO.Stream.

However, that is not what I am getting back!  An IO.Stream is what I need to get back, how can I get it?

Thank you,

Leah

0 Kudos

Hi Leah,

Are you using SP 13? Can you paste in your code? I'll test it also to seem what happens...

Thanks

Don

Former Member
0 Kudos

Hi Don,

Yes, I am using SP 13 as well as Visual Studio 2012.

I have other code that calls the GetPDF function, which then as you can see calls the GenerateReport function.  Before I updated to VS 2012 and Crystal Reports SP 13 this all worked properly.  Nothing in the code has changed, but now the GetPDF function returns a type FileStreamDeleteOnClose instead of an IO.Stream.

   ''' <summary> Function GetPDF creates PDF of this form </summary>

    ''' <param name="genColsolidatedPDF"> = "Yes" if generating a Consolidated PDF </param>

    ''' <returns> IO.Stream </returns>

    ''' <remarks> The class must be instantiated before calling this method. </remarks>

    Protected Friend Function GetPDF(ByVal genColsolidatedPDF As String) As IO.Stream

        Return GenerateReport(genColsolidatedPDF).ExportToStream(ExportFormatType.PortableDocFormat)

    End Function

    ''' <summary>

    ''' Generate report PDF for Consolidated PDF

    ''' </summary>

    ''' <param name="genConsolPDF"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Private Function GenerateReport(ByVal genConsolPDF As String) As ReportDocument

        Dim connInfo As New ConnectionInfo

        Dim databaseLoginInfo As New TableLogOnInfos

        Dim reportDoc As New ReportDocument

        Dim tableSet As Tables

        reportDoc = New ActiveSignatures

        reportDoc.SetParameterValue(DAL.ParmName.MRId, BLL.SessionData.EssInfo.MRID)

        reportDoc.SetParameterValue(cCONSOLIDATEPDF, genConsolPDF)

        connInfo.ServerName = [Lib].ApplicationData.db_Server

        connInfo.DatabaseName = [Lib].ApplicationData.db_Name

        connInfo.UserID = [Lib].ApplicationData.db_UserId

        connInfo.Password = [Lib].ApplicationData.db_Password

        tableSet = reportDoc.Database.Tables

        For Each singleTable As Table In tableSet

            Dim singleTableLogoninfo As TableLogOnInfo = singleTable.LogOnInfo

            singleTableLogoninfo.ConnectionInfo = connInfo

            singleTable.ApplyLogOnInfo(singleTableLogoninfo)

        Next

        Return reportDoc

    End Function

Please let me know if there is anything else you need to help resolve this issue.

Thank you,

Leah

0 Kudos

Hi Leah,

I pinged the Developer about this questions and here is what he suggested to do:

From the code piece I could not see how customer dealt with the exported stream.

The problem now is not that I am trying to get an IO.MemoryStream
out of the ExportToStream function, it is that I used to get an IO.Stream as a
result from the function and now I am getting back a FileStreamDeleteOnClose
instead.  This type, FileStreamDeleteOnClose then is not able to be cast
to an IO.MemoryStream item.

...At least following synchronized stream copy works well for writing a file:

            ReportDocument rd=new ReportDocument();           
            rd.Load(rptPath);
            //System.IO.MemoryStream exportStream = (MemoryStream)rd.ExportToStream(ExportFormatType.CharacterSeparatedValues);
            System.IO.Stream exportStream = rd.ExportToStream(ExportFormatType.Xml);
            using (FileStream fileStream = new FileStream(expPath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                exportStream.CopyTo(fileStream);
                fileStream.Dispose();
            }
            exportStream.Close();

Btw,  if the exported file over 4GB will cause error, it’s a known limitation…

See if this helps.

Don

Former Member
0 Kudos

Hi Don,

I gave this a try and I got the same results I was getting before the change the ExportToStream is returning a object of type FileStreamDeleteOnclose instead of an object of type IO.Stream.

You have what I had before already, but here is what I changed it to in order to do what the developer asked:

Protected Friend Function GetPDF(ByVal genColsolidatedPDF As String) As IO.

Stream

 

Dim exportStream As System.IO.Stream = GenerateReport(genColsolidatedPDF).ExportToStream(ExportFormatType

.PortableDocFormat)

Using fileStream As New IO.FileStream(expPath, System.IO.FileMode.Create, System.IO.FileAccess

.Write)exportStream.CopyTo(fileStream)fileStream.Dispose()

exportStream.Close()

Return

exportStream

End Function

Private Function GenerateReport(ByVal genConsolPDF As String) As

ReportDocument

Dim connInfo As New

ConnectionInfo

Dim databaseLoginInfo As New

TableLogOnInfos

Dim reportDoc As New

ReportDocument

Dim tableSet As

Tables

'reportDoc = New ActiveSignatures

reportDoc.Load(rptPath)

reportDoc.SetParameterValue(DAL.ParmName.MRId, BLL.SessionData

.EssInfo.MRID)

reportDoc.SetParameterValue(cCONSOLIDATEPDF, genConsolPDF)

connInfo.ServerName = [Lib].ApplicationData.db_Server

connInfo.DatabaseName = [Lib].ApplicationData.db_Name

connInfo.UserID = [Lib].ApplicationData.db_UserId

connInfo.Password = [Lib].ApplicationData.db_Password

tableSet = reportDoc.Database.Tables

For Each singleTable As Table In

tableSet

Dim singleTableLogoninfo As TableLogOnInfo

= singleTable.LogOnInfo

singleTableLogoninfo.ConnectionInfo = connInfo

singleTable.ApplyLogOnInfo(singleTableLogoninfo)

Next

Return reportDoc

End Function

It seemed like the developer maybe wanted more information about how the report is then used, so this is what happens.  The report is then supposed to be returned as type IO.Stream and then it is to be converted to an IO.MemoryStream: 

Dim sourceFiles(sourceSize as MemoryStream)

sourceFiles(i) = New MemoryStream(GetByteArray(CType(parameters.SignaturePage, IO.MemoryStream)))

parameters.SignaturePage is the crytal report that is being returned, that is what needs to be IO.Stream, but is coming back as FileStreamDeleteOnclose.

When the application then tries to convert from FileStreamDeleteOnClose to IO.MemoryStream, that is where the error occurrs:  Exception: System.InvalidCastException: Unable to cast object of type 'FileStreamDeleteOnClose' to type 'System.IO.MemoryStream'.  But this is only occurring because I am getting back the incorrect object type from the ExportToStream call. 

Before I upgraded to SP 13 this worked fine, and has been working fine for years.  It is after SP13 that this ExportToStream stopped giving me the IO.Stream object type and is now returning the FileStreamDeleteOnClose.

I hope that helps!

Thank you,

Leah