cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

Hi,

I am using VS 2015 version 14 update 2

Crystal Reports for VS and runtime engine(13.0.21.2533)

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 vb.net. 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")

Try

Dim CrExportOptions As ExportOptions

Dim CrDiskFileDestinationOptions As New _

DiskFileDestinationOptions()

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

objRpt.Export()

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

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

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.

Don

Answers (3)

Answers (3)

Former Member
0 Kudos

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?

Kevin

0 Kudos

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.

Don

Former Member
0 Kudos

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.

Regards,

Kevin