Crystal Transmission Parameters

I create a critical report from a stored procedure, this works fine when I pass one parameter, but it shows an error

"invalid parameter"

when i pass two parameters my code

{ 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

+6
c # crystal-reports
source share
5 answers

You need to create a new Field parameter and a value for both parameters. Your current code adds the parameter, changes it (changes the name and value), and adds the 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: The error mentioned in the comment is probably because there are two definitions of the variable paramField or paramDiscreteValue in the code. In one C # method, you cannot define a variable with the same name more than once. Try the code above as it is written, and if you still get a compiler error, insert the full error text here.

+7
source share

Try this 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"); } 
+4
source share

Make sure that the actual data passed in the parameters is not implicitly converted to the wrong data type. For example, if you pass a numeric identifier for the @Dept parameter, make sure that the data type of the input parameter that expects to receive the value is also numeric.

+2
source share

Try creating a ParameterField parameter before each parameter you add to the report:

 paramField = new ParameterField(); paramDiscreteValue.Value = ... ... 
+2
source share

The problem can be replicated using Crystal Reports for Visual Studio 2005. The fix to solve the problem is to first configure ReportSource for the CrystalReportViewer property and then set the parameter values. So 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"); } 
0
source share

All Articles