Search code examples
c#reportviewerreportparameter

Why are my reports throwing an exception while setting report parameters?


Ok so my scenario is that I have a single report viewer on a form, that connects multiple reports based on a choice. I have all of my reports set as an Embedded Resource for the build action.

I am loading the reports like this before I set my parameters.

myReportViewer.LocalReport.ReportEmbeddedResource = "MyApp.MyReport1.rdlc";

OR

myReportViewer.LocalReport.ReportEmbeddedResource = "MyApp.MyReport2.rdlc";

This situation is very odd. Let's say I run the app and I choose MyReport1 first and run it. This report is the one that holds the parameters. MyReport2 doesn't have any parameters, just data sources. MyReport1 will load correctly and everything runs perfectly. I can then switch to MyReport2 and go back and forth between both reports as many times as I want.

Let's say I run MyReport2 first. It loads correctly and I can run it multiple times. However, if I switch back to MyReport1 it throws the following exception when I try to set the parameters.

An attempt was made to set a report parameter 'TotalTime' that is not defined in this report.

Looking at this exception I would be assuming that MyReport1 didn't load for some reason. When I set the LocalReport.ReportEmbeddedResource. What would cause the MyReport1 not to load correctly just because I hadn't used it first?

Here is the code, I have of course trimmed out pieces that I can't show.

if (ReportComboBox.SelectedItem.ToString() == "Time by user") {
     myReportViewer.LocalReport.DataSources.Clear();
     ReportDataSource datasource = new ReportDataSource();
     datasource.Name = "DataSet1";
     datasource.Value = DataSet1BindingSource;
     myReportViewer.LocalReport.DataSources.Add(datasource);
     try {
          myReportViewer.LocalReport.ReportEmbeddedResource = "";
          myReportViewer.LocalReport.ReportEmbeddedResource = "MyApp.MyReport1.rdlc";
          ReportParameter test = new ReportParameter("TotalTime", total.ToString("c"));
          myReportViewer.LocalReport.SetParameters(test);
          myReportViewer.RefreshReport();
     } catch (Exception ex) { 

     }
} else if (ReportComboBox.SelectedItem.ToString() == "Time - Everyone") {
     myReportViewer.LocalReport.DataSources.Clear();
     ReportDataSource datasource = new ReportDataSource();
     datasource.Name = "CompetitionUsers";
     datasource.Value = MyData;
     myReportViewer.LocalReport.DataSources.Add(datasource);
     myReportViewer.LocalReport.ReportEmbeddedResource = "";
     myReportViewer.LocalReport.ReportEmbeddedResource = "MyApp.MyReport2.rdlc";
     myReportViewer.RefreshReport();
}

Time by user, is the section that is freaking out. By looking at the code there is nothing really helpful there. It works as long as I run it first.


Solution

  • What you need to do is call myReportViewer.Reset() before loading a new report. Doing so will reset the ReportViewer control to its default state and the LocalReport object will be replaced with a new instance. This should solve your problem.

    (Actually, I've just stumbled upon a very similar post that had the same solution :: LocalReport.SetParameters Exception An attempt was made to set a report parameter 'ParameterName' that is not defined in this report)