Skip to Content
avatar image
Former Member

CrystalReports 'System.InvalidCastException' occurred

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'.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jan 14, 2015 at 04:13 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 15, 2015 at 04:19 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Don Williams

      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