C# – LocalReport.SetParameters Exception An attempt was made to set a report parameter ‘ParameterName’ that is not defined in this report

cmicrosoft-reportingparametersrdlcreportviewer

i have two buttons (button1, button2)
the two buttons open two identical report except that report2.rdlc has a string parameter

if i pressed button1 first the message box show parameters count = 0 (as expected) and i get report1.rdlc displayed in the reportviewer1
if then i pressed button2 the message box show parameters count = 0 again (i expect it to show 1) and i get LocalProcessingException {"An attempt was made to set a report parameter 'Report2ParameterString' that is not defined in this report."}

if i pressed button2 first the message box show parameters count = 1 (as expected) and i get report2 displayed in the reportviewer1
if then i pressed button1 the message box show parameters count = 1 again(i expect it to show 0) i get report1.rdlc displayed in the reportviewer1 without exceptions

my code is

private void report1Button_Click(object sender, EventArgs e)
    {
        reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\report1.rdlc";
        MessageBox.Show("parameters count =" + reportViewer1.LocalReport.GetParameters().Count.ToString());
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", reportDataTable));
        reportViewer1.RefreshReport();
    }
    private void report2Button_Click(object sender, EventArgs e)
    {
        reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\report2.rdlc";
        MessageBox.Show("parameters count =" +reportViewer1.LocalReport.GetParameters().Count.ToString());
        reportViewer1.LocalReport.SetParameters(new ReportParameter("Report2ParameterString", " testing Report2ParameterString"));
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", reportDataTable));
        reportViewer1.RefreshReport();
    }

Best Answer

Call ReportViewer.Reset() before loading the new report.

For example:

reportViewer1.Reset();
reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\report2.rdlc";
...
Related Topic