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=, 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


    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.



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


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





      End Function

      Private Function GenerateReport(ByVal genConsolPDF As String) As


      Dim connInfo As New


      Dim databaseLoginInfo As New


      Dim reportDoc As New


      Dim tableSet As


      'reportDoc = New ActiveSignatures


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


      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


      Dim singleTableLogoninfo As TableLogOnInfo

      = singleTable.LogOnInfo

      singleTableLogoninfo.ConnectionInfo = connInfo



      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,