passing parameters in crystal report

柔情痞子 提交于 2020-01-02 03:45:29

问题


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


回答1:


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.




回答2:


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");
}



回答3:


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.




回答4:


Try to create the ParameterField before each parameter that you add to the report:

paramField = new ParameterField();
paramDiscreteValue.Value = ...
...



回答5:


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:

{
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");
}


来源:https://stackoverflow.com/questions/1006873/passing-parameters-in-crystal-report

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