In this tutorial, we are going to discuss how to create a Crystal Report using ASP.NET Web Form applications. We will show you how to create the Typed Dataset and bind the Crystal Report to the Dataset in a Web Form application.
You can read our following tutorial on how to Create Crystal Reports
We are using Visual Studio 2013 and SAP Crystal Reports for Developer Edition to Visual Studio. Click to Download Crystal Report for Visual Studio. The Code also works under Visual Studio 2015 Community Edition and the latest version of Crystal Reports.
We are using the Northwind Database. You can download it from CodePlex
Table of Contents
Creating the Crystal Reports involves the following steps
The First step is to create a Web form application.
Follow these steps to create a Web Forms Project
This will open the New ASP.NET Project Wizard.
In the Previous Tutorial, we learnt how to use OLE DB (ADO) connection. For this example let us choose ADO.NET Dataset. To use ADO.NET Dataset we need to create a dataset in our project. To do that follow the following steps
Add New Item dialogue box appears. Here follow these steps
This will bring Dataset Designer on your screen
Right-click and select Table Adapter.
This will bring up Choose your data connection wizard. The drop-down will display the already created connections. To Create a new connection, click on the New Connection
Click on New Connection
The next dialogue box is Add Connection dialogue box. The first Option in this screen is data source which, is as you can see is displayed as Microsoft SQL Server (SQL Client). If you wish to change it to some other provider then, click on Change. That will take you to the list of available Data Source Select the appropriate one for your project and Click OK
Since we are connecting to the SQL server database let us choose to cancel
Follow these steps to set your database connection
We are taken back to the Choose your data Connection window
The new connection which we created in the previous step appears on drop down. Expand the plus right below connection drop-down see the connection string
You will be prompted to whether you wish to add this connection to the web.config file. Select yes and click Next. This will bring up Choose your Command Type Dialog Box
Here you are given three options to select the Command Type
Let us choose Use SQL Statement. Click on Next. This will bring up Enter SQL Statement dialogue box
Here you are prompted for the SQL statement. Enter our query as
Select * from customersClick on Next. This will take us to the Choose methods to Generate dialogue box
This wizard asks for the methods you want to add to our TableAdapter. Select tick on all three and Click on Next. The dataset is generated for us. Save and close the dataset
Now we have created our dataset. We are now ready to connect this dataset to our crystal report
To Add Crystal Reports to the Report do the following
This will bring up the Crystal Report Gallery
The Gallery offers three options to create report
In the Create your first Crystal Report Using Windows Forms, we showed you how to create the report using the first Option i.e. using the Report wizard. For this example let us create the report as a Blank Report
Select as blank Report and click on Ok
A blank Report is Created and ready for use. Notice that the reference to the following Crystal Report Namespace is added to our project
Now we need to connect this report to the dataset we created. Locate and Open the crystal Report rptCustomerList. To Connect to the dataset do the following
The dataset we have created will appear in the Project Data
The next step is to add the fields to the report. To do this follow these steps
Save the report and close all the open windows.
The next step is to create a web form to show the report. To do this follow these steps
Click on add to create the form
The next step is to add a button to our web form. To do this
The next step is to add crystal Viewer control to our form. To do this
To View the Crystal Report we need to bind the Report (rptCustomer) to the Crystal Viewer Control, which we added to our Web Form. To do this double click on Show Button to open the code behind class
We need to Import following Name Space from the Crystal Reports.
Using CrystalDecisions . CrystalReports . Engine ; Using CrystalDecisions . Shared ;First, We need to Fill the data table with the Customers data. To do that Create an instance of Table Adapter CustomersTableAdapter and invoke the Fill Method to Fill the data.
Create the rpt object, which is an instance of our rptCustomerList. Note the rptCustomerList is the name of the Crystal Report, which we had created in step 3. Assigning the Dataset ds to Report using the method SetDataSource. The object rpt is our Report.
Crystal Report Viewer control’s job is to display the report. All we need to do is to assign the report to the ReportSource Property of the Crystal Report Viewer Control (CrystalReportViewer.ReportSource = rpt). Now our Crystal Report Viewer is ready to display our report.
Finally, store our Report in the Session using Session.Add(“report”, rpt). The Report object is retrieved from the Session and assigned to the ReportSource Property of the CrystalReportViewer Control when the page is refreshed or user navigates from one page to another. This is done from the Page_Init event.
Finally, our code looks like this