C# 在水晶报表中传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1006873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing parameters in crystal report
提问by
I am creating crytal report from a stored procedure ,this works fine when i pass one parameter but it shows an error
我正在从存储过程创建水晶报告,当我传递一个参数时这工作正常,但它显示一个错误
"incorrect parameter "
“不正确的参数”
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");
}
please let me know any changes
请让我知道任何变化
回答by Darth Continent
Make sure the actual data being passed in the parameters isn't being implicitly converted to the wrong data type. Like if you're passing a numeric ID for the @Dept parameter, make sure the data type of the input parameter that expects to receive the value is also a numeric type.
确保在参数中传递的实际数据没有被隐式转换为错误的数据类型。就像您为 @Dept 参数传递数字 ID 一样,请确保期望接收该值的输入参数的数据类型也是数字类型。
回答by zendar
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");
}
}
EDIT: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.
编辑:评论中提到的错误可能是因为代码中有两个变量 paramField 或 paramDiscreteValue 的定义。在一种 c# 方法中,您不能多次定义同名变量。尝试上面编写的代码,如果您仍然遇到编译器错误,请在此处粘贴完整的错误文本。
回答by Nelson Reis
Try to create the ParameterField before each parameter that you add to the report:
尝试在添加到报告的每个参数之前创建 ParameterField:
paramField = new ParameterField();
paramDiscreteValue.Value = ...
...
回答by John Hunter
Try this it is a little more concise
试试这个更简洁一点
{
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
CrystalReportViewer1.ReportSource = reportDocument;
reportDocument.SetParameterValue("@Dept", TextBox1.Text.ToString());
reportDocument.SetParameterValue("@Name", TextBox2.Text.ToString());
// CrystalReportViewer1.ParameterFieldInfo = paramFields;
reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");
}
回答by Christopher Guray
The problem can be replicated with Crystal Reports for Visual Studio 2005. The workaround fix is to set the ReportSource properpty of the CrystalReportViewer first, then you set the parameter values. Thus, your code should be:
该问题可以在 Visual Studio 2005 的 Crystal Reports 中重现。解决方法是首先设置 CrystalReportViewer 的 ReportSource 属性,然后设置参数值。因此,您的代码应该是:
{
ReportDocument reportDocument = new ReportDocument();
CrystalReportViewer1.ReportSource = 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"));
reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");
}