0

我有一个包含两个参数的报告:ponumber 和receiptno。我正在尝试从通过 URL 传入参数值的 C# 页面调用报告。当我调用报告时,我收到一个错误“参数不正确”,但无法弄清楚原因。我根据网上查到的,做了各种代码修改,因为一开始Report Viewer说没有参数,所以这个方法好像更好,但是不行。

我的代码:

string ponumber =  Request.QueryString["ponumber"].ToString();
string receiptno = Request.QueryString["receiptno"].ToString();

    // Put Away Report


    CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
    CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();



    ParameterFields paramFields1 = new ParameterFields();
    ParameterFields paramFields2 = new ParameterFields();
    ParameterField paramField1 = new ParameterField();
    ParameterField paramField2 = new ParameterField();
    ParameterDiscreteValue paramDiscreteValue1 = new ParameterDiscreteValue();
    ParameterDiscreteValue paramDiscreteValue2 = new ParameterDiscreteValue();

    paramField1.Name = "@ponumber";
    paramDiscreteValue1.Value = ponumber;
    paramField1.CurrentValues.Add(paramDiscreteValue1);
    paramField1.HasCurrentValue = true;
    paramFields1.Add(paramField1);

    paramField2.Name = "@receiptno";
    paramDiscreteValue2 = new ParameterDiscreteValue();  // <-- This line is added
    paramDiscreteValue2.Value = receiptno;
    paramField2.CurrentValues.Add(paramDiscreteValue2);
    paramField2.HasCurrentValue = true;
    paramFields2.Add(paramField2);

    CrystalReportViewer1.ParameterFieldInfo = paramFields1;
    CrystalReportViewer1.ParameterFieldInfo = paramFields2;

    TableLogOnInfo logOnInfo = new TableLogOnInfo();

    logOnInfo.ConnectionInfo.ServerName =   ConfigurationManager.AppSettings["WarehouseReportServerName"];
    logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["WarehouseReportDatabaseName"];
    logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["WarehouseReportUserID"];
    logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["WarehouseReportPassword"];

    TableLogOnInfos infos = new TableLogOnInfos();
    infos.Add(logOnInfo);
    CrystalReportViewer1.LogOnInfo = infos;

    maindiv.Controls.Add(CrystalReportSource1);
    maindiv.Controls.Add(CrystalReportViewer1);
    CrystalReportViewer1.ReportSource = CrystalReportSource1;
    CrystalReportViewer1.EnableParameterPrompt = false;
    CrystalReportSource1.Report.FileName = "Report3.rpt";
    CrystalReportSource1.EnableCaching = false;
    CrystalReportViewer1.DataBind();
4

2 回答 2

1

我不熟悉 Crystal Reports,但我突然想到了一些事情,即这里:

    CrystalReportViewer1.ParameterFieldInfo = paramFields1;
    CrystalReportViewer1.ParameterFieldInfo = paramFields2;

您正在覆盖第二行的 ParameterFieldInfo 集合,因此实际上paramFields1永远不会发送到报告中。相反,我认为您想添加到集合中。我已经修改了您的代码来执行此操作(并删除了不必要的行):

    string ponumber = Request.QueryString["ponumber"].ToString();
    string receiptno = Request.QueryString["receiptno"].ToString();

    // Put Away Report

    CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
    CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();

    ParameterField paramField1 = new ParameterField();
    ParameterField paramField2 = new ParameterField();
    ParameterDiscreteValue paramDiscreteValue1 = new ParameterDiscreteValue();
    ParameterDiscreteValue paramDiscreteValue2 = new ParameterDiscreteValue();

    paramField1.Name = "@ponumber";
    paramDiscreteValue1.Value = ponumber;
    paramField1.CurrentValues.Add(paramDiscreteValue1);
    paramField1.HasCurrentValue = true;

    paramField2.Name = "@receiptno";
    paramDiscreteValue2.Value = receiptno;
    paramField2.CurrentValues.Add(paramDiscreteValue2);
    paramField2.HasCurrentValue = true;

    CrystalReportViewer1.ParameterFieldInfo.Add(paramField1);
    CrystalReportViewer1.ParameterFieldInfo.Add(paramField2);

    TableLogOnInfo logOnInfo = new TableLogOnInfo();

    logOnInfo.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["WarehouseReportServerName"];
    logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["WarehouseReportDatabaseName"];
    logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["WarehouseReportUserID"];
    logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["WarehouseReportPassword"];

    TableLogOnInfos infos = new TableLogOnInfos();
    infos.Add(logOnInfo);
    CrystalReportViewer1.LogOnInfo = infos;

    maindiv.Controls.Add(CrystalReportSource1);
    maindiv.Controls.Add(CrystalReportViewer1);
    CrystalReportViewer1.ReportSource = CrystalReportSource1;
    CrystalReportViewer1.EnableParameterPrompt = false;
    CrystalReportSource1.Report.FileName = "Report3.rpt";
    CrystalReportSource1.EnableCaching = false;
    CrystalReportViewer1.DataBind();
于 2012-11-02T18:43:15.793 回答
0

我在 SO 上找到了另一个答案,让我克服了困难。我将 13 行代码替换为:

CrystalReportSource1.ReportDocument.SetParameterValue(0, ponumber);
CrystalReportSource1.ReportDocument.SetParameterValue(1, receiptno);

将值传递给 Crystal 报表

而不是手动构建集合,看起来源有不同的方法来设置参数值。我怀疑我的第一次尝试是动态创建参数。

于 2012-11-02T19:22:41.997 回答