Link to home
Create AccountLog in
Avatar of Marc_Johnson
Marc_Johnson

asked on

Load Report Failed Exception, the second time any report is loaded, Crystal Report for Visual Studio 2010

Hello -

I have a report that runs successfully the very first time I run it through the debugger, then any subsequent attempt to run the report generates a 'Load Report Failed' Exception. Why is this?
I have tried it with a report created in Crystal XI r 2, then with a report created wiht the Crystal Reports embedded in Visual Studio 2010, same error.

Closing Visual Studio resets the application and the report will run one time again successfully.
ReportDocument cryRpt = new ReportDocument();
         frmReportViewer myReportViewer = new frmReportViewer();
         TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
         TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
         ConnectionInfo crConnectionInfo = new ConnectionInfo();
         Tables CrTables;

         ParameterFieldDefinitions crParameterFieldDefinitions;
         ParameterFieldDefinition crParameterFieldDefinition;
         ParameterValues crParameterValues = new ParameterValues();
         ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
            
            //Check Message Result to Save or Cancel
        
             filename = Application.StartupPath + "\\" + "TestCR.rpt";
         
        
         try
         {
             cryRpt.Load(filename);

Open in new window

Avatar of Mike McCracken
Mike McCracken

What is the rest of the code?

What code do you use when you close the viewer?

mlmcc
Avatar of Marc_Johnson

ASKER

I posted the rest of the code, I had left it off because it never gets there. Everything fails when I attempt to Load the report.

I do not have any code for when the viewer closes.
The viewer is a Crystal Reports Viewer on it's own form
private void btnPrintSched_Click(object sender, EventArgs e)
     {
         String messageString;
         String filename = "";

         DialogResult saveData = DialogResult.No;

         string EmployeeName = (string)scheduleDataGridView.CurrentRow.Cells["FullName"].Value;       

         messageString = "Choose YES to print schedule for " + EmployeeName.ToUpper()  + ". Choose NO to print the entire group";                
         
         saveData = MessageBox.Show(messageString, "Print Individual or All Show Schedules?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

         if (saveData == DialogResult.Cancel)
             return;

         //---
         // Report Set Up Info
         //--------
        
         ReportDocument cryRpt = new ReportDocument();
         frmReportViewer myReportViewer = new frmReportViewer();
         TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
         TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
         ConnectionInfo crConnectionInfo = new ConnectionInfo();
         Tables CrTables;

         ParameterFieldDefinitions crParameterFieldDefinitions;
         ParameterFieldDefinition crParameterFieldDefinition;
         ParameterValues crParameterValues = new ParameterValues();
         ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
            
            //Check Message Result to Save or Cancel
         if(saveData == DialogResult.Yes)
         {
             //filename = @"\\lvccmsapps1\reg\RegistrationReports_DotNet\ScheduleIndividual.rpt";
             filename = Application.StartupPath + "\\" + "TestCR.rpt";
         }
         else if(saveData == DialogResult.No)
         {
             //filename = @"\\lvccmsapps1\reg\RegistrationReports_DotNet\ScheduleEvent.rpt";
             filename = Application.StartupPath + "\\" + "TestCR.rpt";
         }

         try
         {

             cryRpt.FileName = filename;
             cryRpt.Load(filename);

             // crConnectionInfo.ServerName = "YOUR SERVER NAME";
             // crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
             crConnectionInfo.UserID = "UserID";
             crConnectionInfo.Password = "Password";

             CrTables = cryRpt.Database.Tables;
             foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
             {
                 crtableLogoninfo = CrTable.LogOnInfo;
                 crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                 CrTable.ApplyLogOnInfo(crtableLogoninfo);
             }

             // SET UP REPORT PARAMETERS 
             //By Default sets the parameters for whole show
             crParameterDiscreteValue.Value = (int)scheduleDataGridView.CurrentRow.Cells["EventID"].Value;
             crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
             crParameterFieldDefinition = crParameterFieldDefinitions["EventID"];
             crParameterValues = crParameterFieldDefinition.CurrentValues;

             if (saveData == DialogResult.Yes)
             {
                 //Over writes parameters with ind Schedule if chosen
                 crParameterDiscreteValue.Value = (int)scheduleDataGridView.CurrentRow.Cells["ScheduleID"].Value;
                 crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
                 crParameterFieldDefinition = crParameterFieldDefinitions["ScheduleID"];
                 crParameterValues = crParameterFieldDefinition.CurrentValues;
             }

             crParameterValues.Clear();
             crParameterValues.Add(crParameterDiscreteValue);
             crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

             //myReportViewer.crystalReportViewer1.ReportSource = cryRpt;
             myReportViewer.crystalReportViewer1.ReportSource = filename;

             myReportViewer.crystalReportViewer1.Refresh();
             myReportViewer.Show();
         }
         catch (Exception Ex)
         {
             MessageBox.Show(Ex.Message);
         }
         finally
         {
              cryRpt.Dispose();
             
         }

     }

Open in new window

I am not familiar with how the TRY - CATCH - FINALLY works

My understanding is that it is basically used to catch the errors so once you enter the TRY part if an error occurs then the CATCH part is run.

If no errors occur does the FINALLY part then run?

Have you closed the viewer?

I assume you click the button
The report runs
WHen you click the button again it prompts for the all or one then you get the error?

mlmcc
Yes, that is how the TRY CATCH FINALLY works, but it is not the issue. I could boil the code down to the simplest items, which would be getting the report file location and then loading the report, which is where the error occurs.

When I close the form viewer I click the X in the corner, the very first tiem through the debugger the report viewer opens and runs. Any subsequent calls and I get the Failure to Load Error no matter which report I try from the select options.

I have simpler code in another location that attempts to open a report on button click and it doesn't not work more than once there either.

private void testReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
           frmReportViewer myReportViewer = new frmReportViewer();
           string filename = Application.StartupPath + "\\" + "TestCR.rpt";

            cryRpt.Load(filename);
           myReportViewer.crystalReportViewer1.ReportSource = cryRpt;
           myReportViewer.crystalReportViewer1.Refresh();
           myReportViewer.Show();
           
         
        }

Open in new window

I asked only because I had similar issues which were solved by ensuring when the form closed and before abnother report was run that the report object was deleted and set to nothing.  That was in VB6.  Since the finally part runs the dispose should cleanup the report object.

i wonder if you have to do the same with the report viewer.

mlmcc
ASKER CERTIFIED SOLUTION
Avatar of Marc_Johnson
Marc_Johnson

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The issue seemed to have resolved itself