Crystal reports The parameter is incorrect C#

独自空忆成欢 提交于 2019-12-13 04:39:38

问题


I have a report that has two parameters: ponumber and receiptno. I'm trying to call the report from a C# page where the parameter values are passed in via the URL. When I call the report, I get an error "The parameter is incorrect", but can't figure out why. I've done a variety of code changes based on what I found online, because at first the Report Viewer said there were no parameters, so this approach seems to be better, but doesn't work.

My code:

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

回答1:


I am not familiar with Crystal Reports, but a few things jumped out at me, namely here:

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

You are overwriting the ParameterFieldInfo collection on the second line, so effectively paramFields1 never gets sent to the report. Instead, I think you want to add to the collection. I have modified your code to do this (and removed the unnecessary lines):

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



回答2:


I found another answer on SO that got me over the hump. I replaced 13 lines of code with:

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

Passing Values to a Crystal Report

Rather than build the collection manually, it looks like the source has a different method to setting parameter values. I suspect my first attempt was to create parameters on the fly.



来源:https://stackoverflow.com/questions/13200234/crystal-reports-the-parameter-is-incorrect-c-sharp

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