0
votes

I have an issue with crystal report in wpf,While continuously opening reports in our application after some time the crystal report viewer stops loading crystal reports again and its throws an exception of:

Load report failed. Unsupported Operation. A document processed by the JRC engine cannot be opened in the C++ stack.

The exception detail is:

CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.EnsureDocumentIsOpened() at CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(String filename, OpenReportMethod openMethod, Int16 parentJob) at CrystalDecisions.CrystalReports.Engine.ReportClass.Load(String reportName, OpenReportMethod openMethod, Int16 parentJob) at CrystalDecisions.CrystalReports.Engine.ReportDocument.EnsureLoadReport() at CrystalDecisions.CrystalReports.Engine.ReportDocument.SetDataSourceInternal(Object val, Type type) at CrystalDecisions.CrystalReports.Engine.ReportDocument.SetDataSource(DataSet dataSet)**

While monitoring the task manager during this time I noticed that, the crystal report I disposed before closing the crystal report viewer window is not releasing the consumed memory for the report/report viewer. This is actually causing the issue. Is there any other way to dispose the crystal report object properly?

Here is the screen shot of exception occurred while loading reports continuously: Image

Here is my code block

CReportManager.cs

private bool GenerateReport(object oValue, bool bNeedPreview)
{
    Reports.crtSampleReport ocrtSample = null ;
    System.Drawing.Printing.PrintDocument doctoprint = null ;

    // its a usercontrol placed on this assembly
    PopUp.ucManagePrintPreview oPrintPreview = null;

    try
    {
        //pass the data to feed the report from other classes ( from same / different assembly ) 
        System.Data.DataSet dsPrintData = (System.Data.DataSet)oValue;

        if (dsPrintData == null || dsPrintData.Tables[2].Rows.Count == 0)
            throw new GOGGeneralHelpers.CGOGCustomException(
                "Print Data is Null", "No Data Available To Print", GOGGeneralHelpers.EnumLogType.ERR);

        ocrtSample = new Reports.crtSampleReport( ) ;
        ocrtSample.SetDataSource( dsPrintData ) ;
        ocrtSample.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait;

        System.Windows.Forms.DialogResult enDialogResult = System.Windows.Forms.DialogResult.OK;

        if (bNeedPreview)
        {     
            // if user want to view the preview before printing the report then its loads 
            // to a crystal report viewer placed on a usercontrol on this same assembly

            oPrintPreview = new PopUp.ucManagePrintPreview();
            oPrintPreview.FillReport( ocrtSample ) ;

            //a Window object placed on another assembly
            EHRPresentationBaseClassLibrary.Popups.PopupWindow
                        oPopupWindow = new EHRPresentationBaseClassLibrary.Popups.PopupWindow( ) ;

            //add the user control object (with crystal report viewer) to this window object            
            oPopupWindow.SetPopupPage( oPrintPreview ) ;
            oPopupWindow.Title = "Print Preview";
            oPopupWindow.Width = GOGPresentationCommonClassesLibrary.CCurrentAppSettings.CurrentAppSettings.PageWidth * .9;
            oPopupWindow.Height = System.Windows.SystemParameters.PrimaryScreenHeight * .8;
            oPopupWindow.ShowDialog();
            enDialogResult = oPrintPreview.PageResult ;                   
        }

        if (enDialogResult == System.Windows.Forms.DialogResult.Cancel
                    || enDialogResult == System.Windows.Forms.DialogResult.None) return true;

        //// Read printer name from a config file ///////////////////////

        System.IO.StreamReader oReader = System.IO.File.OpenText("PrintConfig.bat");
        string sPrinterName = oReader.ReadLine();
        oReader.Close();

        doctoprint = new System.Drawing.Printing.PrintDocument( ) ;
        doctoprint.PrinterSettings.PrinterName = sPrinterName ;

        //validate readed printer name is valid or not
        if ( !doctoprint.PrinterSettings.IsValid )
            throw new GOGGeneralHelpers.CGOGCustomException(
                "Selected Printer is not Valid", "Selected Printer is not Valid", GOGGeneralHelpers.EnumLogType.ERR ) ;

        ///////////////////////////////////////////////////////////////

        ocrtSample.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;

        ocrtSample.PrintOptions.PrinterName = sPrinterName;
        ocrtSample.PrintToPrinter(0, false, 0, 0);
        bReturn = true ;
    }
    catch (GOGGeneralHelpers.CGOGCustomException oCGOGCustomException)
    {                
        GOGPresentationCommonClassesLibrary.CApplicationHelper.
                    ApplicationHelper.ShowUserMessage(oCGOGCustomException, false, false);
        GOGGeneralHelpers.CGOGClientLogWriter.ClientLogWriter.WriteLog(oCGOGCustomException);
    }
    catch (System.Exception oException)
    {
        //Write to log file
        GOGGeneralHelpers.CGOGClientLogWriter.ClientLogWriter.WriteLog(oException);

        //Showing message to user
        GOGPresentationCommonClassesLibrary.CApplicationHelper.
                    ApplicationHelper.ShowUserMessage("Unexpected error on printing", oException, false, false);
    }

    // disposing the crystal report object
    // here is the issue its not releasing consumed memeory
    ocrtSample.Close(); 
    ocrtSample.Dispose();
    ocrtSample = null;

    doctoprint.Dispose();
    doctoprint = null;

    // disposing the crystal report viewer object
    oPrintPreview.ClearReportViewer();

    //this is also not working
    System.GC.Collect();
}

PopUp.ucManagePrintPreview.cs

//Function to load selected report to crystal report viewer
public void FillReport(CrystalDecisions.CrystalReports.Engine.ReportClass oReport) 
{
    MyCrystalReportViewer.ReportSource = oReport;
    MyCrystalReportViewer.ShowPrintButton = true;
}

//Function for dispose crystal report viewer object 
public void ClearReportViewer()
{
    MyCrystalReportViewer.ReportSource = null;
    MyCrystalReportViewer.Dispose();            
}
1

1 Answers

0
votes

I assume the real problem here is the exception. The memory is handled by GC. There's probably not much on the unmanaged side to delete or free, that's why the memory in the task manager will likely not change when a few bytes are released.

Aside from that I've observed that this excepion only happens if Crystal-Reports cannot find the report(-file) you provided, similar to a FileNotFoundException. The stacktrace you provided also shows many calls to various CR-internal Load methods.