Home arrow C# arrow Page 6 - Creating Graphical Reports With Crystal Reports in .NET
C#

Creating Graphical Reports With Crystal Reports in .NET


In this article Farhan Zia investigates some of the power that Microsoft .NET provides to accomplish converting business data into reports using Crystal Reports.

Author Info:
By: Wrox Team
Rating: 4 stars4 stars4 stars4 stars4 stars / 202
November 10, 2002
TABLE OF CONTENTS:
  1. · Creating Graphical Reports With Crystal Reports in .NET
  2. · The Database
  3. · Creating Web Pages
  4. · The Report
  5. · Creating Report Parameters
  6. · Passing Parameters to a Report
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials