问题描述
我正在从存储过程创建水晶报告,当我传递一个参数时效果很好,但它显示错误
I am creating crytal report from a stored procedure ,this works fine when i pass one parameter but it shows an error
参数错误"
当我传递两个参数时我的代码是
when i pass two parameters my code is
{
ReportDocument reportDocument = new ReportDocument();
ParameterField paramField = new ParameterField();
ParameterFields paramFields = new ParameterFields();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "@Dept";
paramDiscreteValue.Value = TextBox1.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField.Name = "@Name";
paramDiscreteValue.Value = TextBox2.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
CrystalReportViewer1.ParameterFieldInfo = paramFields;
reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
CrystalReportViewer1.ReportSource = reportDocument;
reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");
}
请告诉我任何更改
推荐答案
您需要为这两个参数创建新的 parameterField 和 value.您当前的代码添加参数,修改它(更改名称和值)并再次添加相同的对象.这应该是正确的:
You need to create new parameterField and value for both parameters. Your current code adds parameter, modifies it (change name and value) and adds same object again. This should be correct:
{
ReportDocument reportDocument = new ReportDocument();
ParameterFields paramFields = new ParameterFields();
// ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "@Dept";
paramDiscreteValue.Value = TextBox1.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField = new ParameterField(); // <-- This line is added
paramDiscreteValue = new ParameterDiscreteValue(); // <-- This line is added
paramField.Name = "@Name";
paramDiscreteValue.Value = TextBox2.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
CrystalReportViewer1.ParameterFieldInfo = paramFields;
reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
CrystalReportViewer1.ReportSource = reportDocument;
reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");
}
评论中提到的错误可能是因为代码中有两个变量paramField或paramDiscreteValue的定义.在一种 c# 方法中,您不能多次定义具有相同名称的变量.尝试上面编写的代码,如果仍然出现编译器错误,请在此处粘贴完整的错误文本.
Error mentioned in comment is probably because there are two definitions of variable paramField or paramDiscreteValue in code. In one c# method you can't define variable with same name more than one time. Try code above as it is written and if you are still getting compiler error, please paste here full error text.
这篇关于在水晶报表中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!