LocalReport.SetParameters Exception尝试设置此报告中未定义的报告参数“ParameterName”

时间:2012-02-17 22:21:40

标签: c# parameters reportviewer rdlc microsoft-reporting

我有两个按钮(button1,button2) 除了report2.rdlc有一个字符串参数

之外,这两个按钮打开两个相同的报告

如果我先按下button1 消息框显示参数count = 0(如预期的那样),我会在reportviewer1中显示report1.rdlc 如果然后我按下button2 消息框再次显示参数count = 0(我希望它显示1),我得到LocalProcessingException {“试图设置一个报告参数'Report2ParameterString'不是在本报告中定义。“}

如果我先按下button2 消息框显示参数count = 1(按预期方式),我会在reportviewer1中显示report2 如果然后我按下button1 消息框再次显示参数count = 1(我希望它显示0)我在reportviewer1中显示report1.rdlc而没有例外

我的代码是

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 个答案:

答案 0 :(得分:5)

在加载新报告之前致电ReportViewer.Reset()

例如:

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