LocalReport.SetParameters Exception An attempt was made to set a report parameter 'ParameterName' that is not defined in this report

孤人 提交于 2019-12-10 18:53:13

问题


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();
    }

回答1:


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

For example:

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


来源:https://stackoverflow.com/questions/9336272/localreport-setparameters-exception-an-attempt-was-made-to-set-a-report-paramete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!