Skip to Content

Why can I not consistently programatically export .rpt file to Excel or PDF?

Sep 18, 2017 at 03:32 PM


avatar image
Former Member


I am using VS 2015 version 14 update 2

Crystal Reports for VS and runtime engine(

Windows Forms application. Targetting .Net Framework 4.5.2

I am having a real problem with trying to reliably export a crystal report (to a pdf or xls) in I programatically create a report in my code based on an existing report (which was created in Visual Studio). The existing report has sub reports (all verified and data source confirmed to be looking at data tables in datasets in my visual studio application). I independently retrieve data from SQL server and put this data into tables. I then set the datasources of the subreports to the tables with the populated SQL data. (not sure if this is the best approach but it works OK in terms of creating a report). After exporting I close and dispose of the report and all objects in the winform and close and dispose of the winform itself. I then recreate the winform and try to export again - this process can work up to a few times but I eventually get an error. The problem only occurs on the export function. I can load the report repeatedly without issue (if I don't try to export). I am using the '.export' function for crystal reports:

Public Sub ExportToExcel()

Dim d As DateTime d = LabelShiftStart.Text.Trim

Dim s As String = d.ToString("ddMMMyyyy_HHmm")


Dim CrExportOptions As ExportOptions

Dim CrDiskFileDestinationOptions As New _


Dim CrFormatTypeOptions As New ExcelFormatOptions

ShiftReportNameAndPathExcel = "C:\iLine Reports\Report_" & s.Trim & ".xls"

CrDiskFileDestinationOptions.DiskFileName = "C:\iLine Reports\Report_" & s.Trim & ".xls" CrExportOptions = objRpt.ExportOptions

With CrExportOptions .ExportDestinationType = ExportDestinationType.DiskFile .ExportFormatType = ExportFormatType.Excel .DestinationOptions = CrDiskFileDestinationOptions .FormatOptions = CrFormatTypeOptions

End With

' check first in case file already exists

If Not File.Exists(ShiftReportNameAndPathExcel) Then

' only export if file name does not already exist


End If

Catch ex As Exception

TraceUpdate(TimeOfDay.Now.ToString("dd/MMM/yy HH:mm:ss:fff") & "Failure to export to Excel." & ex.ToString.Trim)

End Try

End Sub

The first export works without issue always. but subsequent (sometimes second or later) have resulted in the following error:

16/Sep/17 20:59:26:046SHIFT REPORT Failure to export to pdf.CrystalDecisions.CrystalReports.Engine.DataSourceException: Error in File CrystalReportShift 4312_4644_{0DFEA3E2-2AC7-4E57-AC88-FE9A3A722F51}.rpt: The table could not be found. ---> System.Runtime.InteropServices.COMException: Error in File CrystalReportShift 4312_4644_{0DFEA3E2-2AC7-4E57-AC88-FE9A3A722F51}.rpt: The table could not be found. at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext) at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) --- End of inner exception stack trace --- at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e) at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext) at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext) at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export() at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export() at ?.?()

when the application crashes VS logs the following in the output screen:

Exception thrown at 0x5D6ABCCF (clr.dll) in IlineSQLEngine.exe: 0xC0000005: Access violation reading location 0x00000000. The Common Language Runtime cannot stop at this exception. Common causes include: incorrect COM interop marshalling and memory corruption. To investigate further use native-only debugging. An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.

Each time I am executing the same code with the same data so I don't know why it does not work? Essentially the logic of my code seems good but something is going on to cause it to crash as a result of subsequent attempts to export.

I've spent a few days on this and I am badly stuck so any help would be appreciated. Regards, Kevin

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Don Williams
Sep 18, 2017 at 03:37 PM

Hi Kevin,

Are you closing/disposing of the data source also?

The error suggests there is a problem in that part of the code.

In these 2 lines try to hard code the full path and file name rather than dynamically:

ShiftReportNameAndPathExcel = "C:\iLine Reports\Report_" & s.Trim & ".xls"

CrDiskFileDestinationOptions.DiskFileName = "C:\iLine Reports\Report_" & s.Trim & ".xls"

Could be the file still exists or some temp file etc.

Delete your temp file in the users temp folder also, could be something left over in there.


10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Sep 20, 2017 at 09:16 AM

Hi Don,

I originally had my code doing the following:

1. Retrieve data from SQL Server.

2. Create and populate tables.

3. Set crystal report data source to tables in step 2.

4. Set crystal report viewer source to tables in step 2.

5. Dispose of data sources.

6. Export from crystal report.

I think the problem was disposing of the data sources too soon. The crystal report viewer data (and export) was unaffected by this early disposing but I believe trying to export from the report is an issue.

I've changed the code so that I don't dispose of the datasources until I am disposing the crystal report. I have not seen the issue reoccur so far. You might let me know if you agree with my logic above?

One other question:

I have all code wrapped in (try / catch ex as exception / end try) The errors I encountered were not caught by my own code. This code will also run in a windows service. Could you advise me how I could catch these errors (for the sake of making the application robust/ just in case) without having the application crash? Thanks for pointing me in the right direction.



10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Sep 21, 2017 at 10:47 AM

Thanks Don,

I currently check that the report exists before trying to export.

Could you clarify what you mean by 'just test if it's not null' .

Do you mean test the data source - could you give me a sample line of code to do this to clarify?


10 |10000 characters needed characters left characters exceeded
Don Williams
Sep 20, 2017 at 12:56 PM

Hi Kevin,

Since it's a service you'll have to log the error into a text file. Try using the try catch on each call and you may want to use the InnerException, just test if it's not null so it doesn't generate an error.


10 |10000 characters needed characters left characters exceeded