How to use Crystal Reports in ASP.NET using C#

We will first download SAP Crystal Reports from the source and install it on our system, then using some simple code we will proceed.

Initial chamber

First we need to download the SAP Crystal Reports for respective IDE’s of Visual Studio as in the following:

Step 1

Open Your Visual Studio 2010 and create an Empty Website, provide a suitable name (crystalrpt_demo).

Step 2

In Solution Explorer you get your empty website and then add a Web Form and SQL Server Database and Dataset as in the following:

For Web Form

crystalrpt_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it crystalrpt_demo.aspx.

For SQL Server Database

crystalrpt_demo (your empty website) then right-click then right-click then seelctAdd New Item -> SQL Server Database. (Add the database inside the App_Data_folder).

For DataSet

crystalrpt_demo (your empty website) then right-click then right-click then seelct Add New Item -> DataSet (add the DataSet inside the App_Code_folder).

Database chamber

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table then make the table like this:

  • Table ->tbl_data (Don’t forget to make ID as IS Identity -- True).

    table design

    Insert some data into your table by going to your table tbl_data then right-click then select Show tbl_data -> Add data in the table.

  • Stored Procedure – > sp_select:

    procedure

Double-click on the dataset that is resides under the App_code_Folder, see the following Image.

dataset

When you double-click the dataset you get the blank area, there you need to do something like the following.

Right-click on the blank area then seelct Add -> Datatable.

Add Datatable

add new data table

Change the name of the table from Datatable1 to Newtbl_data. There you must add 3 columns for ID, Name and City as we did in tbl_data, so add the three columns. You will get something as in the following image.

new table

You must then go to crystalrpt_demo then right-click then select Add New Item -> Find Crystal Report.rpt file.

Crystal Report

When you add this new Crystal Report Gallery opened there you need to do as in the following image.

add this new Crystal Report

You will have something like this:

Gallery

When your Crystal Reports report is opened as in the preceding image, you will find in the left or right dock Field Explorer is opened.

database field

Right-click on Field Explorer then select Database Expert -> Create New Connection. You will need to find the table that we made in the Dataset, Newtbl_data.

Once you have your table, add that table to the right side pane using the “ >>” button then press OK as in the following image.

Add that table

You will get you Newtbl_data in Field Explorer like this.

Field Explorer

Here you must add these ID, Name and City from Database Fields to the Crystal Reports report, Section 3 Page Details. You can drag these fields to the Crystal Report Page Details.

Crystal Report Page Details

Design chamber

Step 4

Now open your crystalrpt_demo.aspx file, where we create our design for Crystal Reports. We will add a Crystal Reports Viewer here.

Go to the toolbox and find Reporting then Drag and Drop a Crystal Reports Viewer.

crystalrpt_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"   
  16.             AutoDataBind="true" />  
  17.       
  18.     </div>  
  19.     </form>  
  20. </body>  
  21. </html>  

Code chamber

Step 5

Open your crystalrpt_demo.aspx.cs and write some code so that the application works.

crystalrpt_demo.aspx.cs

Add some namespaces to the crystalrpt_demo.aspx.cs page as in the following:

name space

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using CrystalDecisions.CrystalReports.Engine;  
  9. using System.Data.SqlClient;  
  10.   
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13.     ReportDocument rprt = new ReportDocument();  
  14.       
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         rprt.Load(Server.MapPath("~/CrystalReport.rpt"));  
  18.   
  19.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  20.         SqlCommand cmd = new SqlCommand("sp_select", con);  
  21.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  22.         DataSet ds = new DataSet();  
  23.         sda.Fill(ds, "Newtbl_data");  
  24.         rprt.SetDataSource(ds);  
  25.         CrystalReportViewer1.ReportSource = rprt;  
  26.           
  27.               
  28.     }  
  29. }  
Output chamber

Output

I hope you like this. Thank you for reading. Have a good day.