Programming Crystal Reports with ASP.NET 2.0
Sunday, February 17, 2008 4:57:53 PM
This is the first article in a series focusing on programming with Crystal Reports with ASP.NET 2.0. In this article, we will focus on adding a Crystal Report to an ASP.NET 2.0 web site and displaying the same using the CrystalReportViewer control in a number of different ways.
Starting an ASP.NET 2.0 Web Site Project
The following are the steps necessary to create a new ASP.NET 2.0 web site:
* Open Microsoft Visual Studio 2005 Professional Edition
* Open File || New || Web Site

* Select "ASP.NET Web Site" in templates, select "File System" as the location, provide the website
name "SampleWebSite01" and finally click on OK.

* Right click on "SampleWebSite01" available in Solution Explorer and select "Add New Item."

In the previous section, a new ASP.NET web site was created. In this section, we shall walk through the creation of a Crystal report. The following are the steps you need to take; they pick up from where we left off in the previous section:
In the "Add New Item" dialog box, select "Crystal Report" as the template, provide the name "SampleRpt01.rpt" and click on the "Add" button.

You should see "SampleRpt01.rpt" added to the "Solution Explorer" and the "Crystal Reports Gallery" dialog will be shown (Fig 05). Select "Standard" in "Choose an Expert" and click OK (Fig 05).

Next, you will be taken to provide details for the Data Source. In the "Available Data Sources," open "Create New Connection" and open "OLE DB (ADO)." It brings up another dialog box to let you select the OLEDB Provider. Select "Microsoft OLEDB Provider for SQL Server" in the list of Providers and click on "Next" as shown below.

Provide the db connection information to connect to the Northwind database and click on "Finish."
A new connection gets added to the "OLEDB (ADO)" group. Open the added connection and further drill down to Northwind || dbo || Tables, add the "Orders" table to the "Selected Tables" list (by selecting it and clicking on the greater than arrow), and click on "Next" as shown below.

In the next screen of the wizard, you will be provided with all the fields (or columns) to include in the report. Add OrderID, CustomerID, EmployeeID and OrderDate to the "Fields to Display" section by double clicking each of those fields in the "Available Fields" list and click on "Finish" as shown below.

In the previous section, we added a new Crystal Report to the web site. Once the report gets added, you should be able to see the Visual Studio layout with "Field Explorer," the new "Crystal Reports" menu and an "Embedded Crystal Report Designer" with two modes ("Main Report" and "Main Report Preview" at the bottom) as shown below.

The "Field Explorer" can be used to drag and drop the columns onto the report, add new, add new tables to the report, add new formula oriented columns, running totals, and so on.
The report designer is initially divided into five sections (Report Header, Report Footer, Page Header, Page Footer and Details). Report Header gets displayed only on the first page. Report Footer gets displayed only on the last page. Page Header gets displayed on top of every page. Page Footer gets displayed at the bottom of every page. The Detail section is the body of the report where all of the rows get displayed.
Using the formatting toolbar, properties windows and tools in toolbox, we can format the report in the required manner. Try playing with formatting the report (and preview it simultaneously without executing the project).
Displaying Crystal Report on the ASP.NET 2.0 web page
To display the report on the web page, we need to work with Crystal Report Viewer control. The following are the steps required to attach the Crystal Report to the ASP.NET 2.0 web page:
Using the ToolBox, drag and drop the CrystalReportViewer control.
Using the Smart tag of the CrystalReportViewer control, select "New Report Source."
This leads you to a new dialog box, "Crystal Report Source." Every CrystalReportViewer gets associated with the CrystalReportSource control. You can directly drag and drop the CrystalReportSource control from the toolbox as well and assign the same in a smart tag. Provide the details and click OK.
The moment you hit OK, you should be previewing the report. Just hit F5 to execute your web site. Once the web page gets displayed, provide logon information for the report and click on "Logon."






