我正在从存储过程创建crytal报告,当我传递一个参数但它显示错误时这很好用
“参数不正确”
当我传递两个参数时 我的代码是
{
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");
}
请让我知道任何变化
答案 0 :(得分:8)
您需要为这两个参数创建新的parameterField和value。您当前的代码添加参数,修改它(更改名称和值)并再次添加相同的对象。这应该是正确的:
{
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#方法中,您不能多次定义具有相同名称的变量。 尝试编写上面的代码,如果仍然遇到编译器错误,请在此处粘贴完整的错误文本。
答案 1 :(得分:4)
试试这个更简洁
{
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");
}
答案 2 :(得分:2)
确保参数中传递的实际数据未被隐式转换为错误的数据类型。就像您为@Dept参数传递数字ID一样,请确保期望接收该值的输入参数的数据类型也是数字类型。
答案 3 :(得分:2)
尝试在添加到报告的每个参数之前创建ParameterField:
paramField = new ParameterField();
paramDiscreteValue.Value = ...
...
答案 4 :(得分:0)
可以使用Crystal Reports for Visual Studio 2005复制该问题。解决方法修复方法是首先设置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");
}