Generate ASP.NET Crystal Report using DataSet

This article will give a clear idea of how to create Crystal Report in ASP.NET/C#. We can create a crystal using the following steps: 

  1. Create a Dataset and define the schema by drag and drop the database table from Server Explorer.
    If there are multiple tables then put all the tables within one dataset itself.

    Right Click Solution Explorer -> Add -> Add New Item -> choose DataSet under the Categories (Web Project Items - data).

    Add new Connection in the Server Explorer and expand the connection to retrieve the database tables and choose the required table and drag and drop it in the Dataset xsd pane.

  2. Generate Dataset from the Dataset XSD.

    Right click on the dataset xsd pane and click Generate Dataset

  3. Create Crystal Report.

    Right Click Solution Explorer -> Add -> Add New Item -> choose Crystal Report under the Categories (Web Project Items).

  4. Configure the Crystal Report.

    1. Select Report Layout, ProjectData, ADO.NET DataSets
    2. Expand ADO.NET DataSets and select the table
    3. Select the fields 

  5. Create a WebForm and drag and drop the CrystalReportViewer control from the Toolbox(General).

  6. Put a textbox and button 

  7. Open the Code window of the WebForm and write the following code.

    And change the value of the connectionstring variable sqlConn. 



using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


namespace CrystalReportEg


          /// <summary>

          /// Summary description for WebForm1.

          /// </summary>

          public class WebForm1 : System.Web.UI.Page


                   protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;

                   protected System.Web.UI.WebControls.Label Label1;

                   protected System.Web.UI.WebControls.TextBox TextBox1;

                   protected System.Web.UI.WebControls.Button Button1;

                   public Customer oRpt = null;


                   private void Page_Load(object sender, System.EventArgs e)


                             // Put user code to initialize the page here




                   #region Web Form Designer generated code

                   override protected void OnInit(EventArgs e)



                             // CODEGEN: This call is required by the ASP.NET Web Form Designer.




                             oRpt = new Customer();




                   /// <summary>

                   /// Required method for Designer support - do not modify

                   /// the contents of this method with the code editor.

                   /// </summary>

                   private void InitializeComponent()


                             this.Button1.Click += new System.EventHandler(this.Button1_Click);

                             this.Load += new System.EventHandler(this.Page_Load);





                   private void Button1_Click(object sender, System.EventArgs e)




                   protected void GenerateReport()


                             SqlConnection sqlConn = new SqlConnection("Server=localhost;uid=sa;password=;initial



                             SqlCommand comd;

                             comd = new SqlCommand();

                             comd.Connection = sqlConn;

                             comd.CommandType = CommandType.StoredProcedure;

                             comd.CommandText = "up_GetAllCustomer";








                             SqlDataAdapter sqlAdapter = new SqlDataAdapter();

                             sqlAdapter.SelectCommand = comd;


                             Dataset1 ds = new Dataset1();

                             sqlAdapter.Fill(ds, "Customers");


                             oRpt.SetDataSource (ds);




                             CrystalReportViewer1.ReportSource = oRpt;





Hope this will give you a clear picture of the web crystal report generation.


Enjoy Coding. 

Up Next
    Ebook Download
    View all
    View all