Creating Graphical Reports With Crystal Reports in .NET - Passing Parameters to a Report
(Page 6 of 7 )
If you take a look at the end of the Page_Load function, we have called a method setReportParameters(). This method will actually add/send the parameters to our report. So let us see this final function:
// this method sets the parameters of the report
private void setReportParameters()
{
// all the parameter fields will be added to this collection
ParameterFields paramFields = new ParameterFields();
// the parameter fields to be sent to the report
ParameterField pfItemId = new ParameterField();
ParameterField pfStartDate = new ParameterField();
ParameterField pfEndDate = new ParameterField();
// setting the name of parameter fields with wich they will be recieved in report
pfItemId.ParameterFieldName = "ItemId";
pfStartDate.ParameterFieldName = "StartDate";
pfEndDate.ParameterFieldName = "EndDate";
// the above declared parameter fields accept values as discrete objects
// so declaring discrete objects
ParameterDiscreteValue dcItemId = new ParameterDiscreteValue();
ParameterDiscreteValue dcStartDate = new ParameterDiscreteValue();
ParameterDiscreteValue dcEndDate = new ParameterDiscreteValue();
// setting the values of discrete objects
dcItemId.Value = nItemId;
dcStartDate.Value = DateTime.Parse(strStartDate);
dcEndDate.Value = DateTime.Parse(strEndDate);
// now adding these discrete values to parameters
pfItemId.CurrentValues.Add(dcItemId);
pfStartDate.CurrentValues.Add(dcStartDate);
pfEndDate.CurrentValues.Add(dcEndDate);
// now adding all these parameter fields to the parameter collection
paramFields.Add(pfItemId);
paramFields.Add(pfStartDate);
paramFields.Add(pfEndDate);
// finally add the parameter collection to the crystal report viewer
crViewer.ParameterFieldInfo = paramFields;
} Before dissecting the above code let us study the theory behind it. Our crystal report viewer object crViewer has a property that accepts a collection of ParameterFields type. Through this property, all the parameters are passed to the report. This ParameterFields collection has an Add method, which accepts objects of type ParameterField. So what we do is create three objects of type ParameterField for our three parameters, ItemId, StartDate and EndDate. We then add the values of these parameters to these objects, add them to the collection, and then finally specify this collection as the ParameterFieldInfo property of the crystal report viewer object.
So, from the top where we declare the collection and the three ParameterField objects:
// all the parameter fields will be added to this collection
ParameterFields paramFields = new ParameterFields();
// the parameter fields to be sent to the report
ParameterField pfItemId = new ParameterField();
ParameterField pfStartDate = new ParameterField();
ParameterField pfEndDate = new ParameterField(); Then we name the parameter fields. These are the names with which they are referred in the crystal report. Remember, we used these names there. After this we need to provide values to these parameter fields. But we cannot just set the values of these parameter fields directly - they accept object of type ParameterDiscreteValue. So we will declare three objects of the this type, and after specifying their values, will add them to the parameter fields. This code deals with this task:
// the above declared parameter fields accept values as discrete objects
// so declaring discrete objects
ParameterDiscreteValue dcItemId = new ParameterDiscreteValue();
ParameterDiscreteValue dcStartDate = new ParameterDiscreteValue();
ParameterDiscreteValue dcEndDate = new ParameterDiscreteValue();
// setting the values of discrete objects
dcItemId.Value = nItemId;
dcStartDate.Value = DateTime.Parse(strStartDate);
dcEndDate.Value = DateTime.Parse(strEndDate);
// now adding these discrete values to parameters
pfItemId.CurrentValues.Add(dcItemId);
pfStartDate.CurrentValues.Add(dcStartDate);
pfEndDate.CurrentValues.Add(dcEndDate); Here we first declare these objects, then set their values and add them to the parameter fields. As you can see, we have called the Add method of the CurrentValues object and passed it the discrete parameters. So this way all the parameter fields are ready to be added to the collection. Here is how this is done:
// now adding all these parameter fields to the parameter collection
paramFields.Add(pfItemId);
paramFields.Add(pfStartDate);
paramFields.Add(pfEndDate); And finally, we pass this collection to the ParameterFieldInfo property of our crystal report viewer object.
// finally add the parameter collection to the crystal report viewer
crViewer.ParameterFieldInfo = paramFields; We are finished. Build your project and run it. If you have previously added some sale entries in your tables then you can call ManagerDefault.aspx directly to create a report; otherwise first add some records.
You should see the following output in your browser:
We can change the look of the report by changing the ViewReport.aspx page. By making some slight variations in the report, like changing chart type by clicking on the report and selecting ChartExpert, we can obtain some different charts.
Next: Conclusion >>
More C# Articles
More By Wrox Team