问题
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