In many of our applications, we need to generate a report using Crystal Report(CR) to retrieve data into a certain format. The report can have as data source a XML Document, a DataSet, a Table or even a Stored Procedure from a database. This article describes how to pass the required parameters of a Stored Procedure that is being used as data source by a CR report. If these parameters are not passed through code when loading the report viewer (crystalReportViewer class), a dialog will be shown requesting each necessary parameter for execute the Store Procedure.
If we want, we can avoid this dialog and dynamically enter all the parameters in the code.
The fallowing C# code avoid that:
//
// We need declare this namespace
//
using CrystalDecisions.Shared;
The code of below needs to be written after the ReportSource porperty of the crystalReportViewer has been set and before the control to which belongs finishes being loaded.In this case, we are adding two parameters
ParameterField field1 = this.crystalReportViewer1.ParameterFieldInfo[0];
ParameterField field2 = this.crystalReportViewer1.ParameterFieldInfo[1];
ParameterDiscreteValue val1 = new ParameterDiscreteValue();
ParameterDiscreteValue val2 = new ParameterDiscreteValue();
val1.Value = "parameter1";
val2.Value = "parameter2";
field1.CurrentValues.Add(val1);
field2.CurrentValues.Add(val2);
That's all we need to do.