This is the fourth article of a series that uses ASP.NET and
Crystal Reports to build reports using the Adventure Works Sample Database. Before
reading this article it would be helpful to have read Part
1, Part
2, or Part
3, but it is not required. This article focuses on creating an ASP.NET web application
that allows a user to dynamically change the grouping on a report without
having to create a new report file. How many times have you created a report
for a user and once they start using it they ask you to give them the same data
just grouped a different way? You may want to show a count of records or a
summary in the group footer. This is especially true when working with dates. Users
ask to see the same data by year, quarter, month, or week. You could create a
separate report file for each group, but this can be difficult to maintain. Instead,
Crystal Reports for .NET allows you to manipulate the report at runtime so the
user can change the group themselves without creating multiple copies of the
report.
Before you begin you will need to have installed Visual
Studio 2008 with Crystal Reports for .NET. The samples are written in Visual
Studio 2008, but they will work with Visual Studio 2005 as well. You also will
need to download the AdventureWorks sample database from http://msftdbprodsamples.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=18407
for SQL Server 2008. Download and install the
SQL2008.AdventureWorks_All_Databases.x86.msi file. If you do not have SQL
Server 2008, you can use SQL Server 2005, but you will need to download the
2005 AdventureWorks samples.
The goal of this article is to create a web page that looks
like the following image.
Figure 1
The user can change the group in the report by either using
a database field or a formula field. The Tables and Fields drop down list are
populated with the tables and fields that are defined in the Crystal Report
file. Clicking the "Preview With DB Fields" button will change the
group in the report to use the field selected in the drop down list. The
Formula Fields drop down list is populated with all the formulas in the report.
This particular report has formula fields for the Year, Quarter, Month, and
Week for each employee's date of hire. The "Preview With Formula Fields"
button changes the group in the report to the selected item in the Formula
Fields drop down list. The report is displayed in the web page by using the
Crystal Report Viewer control.