SIGN UP MEMBER LOGIN:    
ARTICLE

Generate ASP.NET Crystal Report using DataSet

Posted by Subhajit Datta Roy Articles | Crystal Reports C# March 02, 2006
This article will help us to create Crystal Report in ASP.NET/C# using Dataset.
Reader Level:
Download Files:
 

This article will give a clear idea of how to create Crystal Report in ASP.NET/C#. We can create a crystal report.net 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.

    STEPS:
    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.

    STEPS:
    Right click on the dataset xsd pane and click Generate Dataset

  3. Create Crystal Report.

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

  4. Configure the Crystal Report.

    STEPS:
     
    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. 

Webform1.aspx.cs

 

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.

                             //

                             InitializeComponent();

                             base.OnInit(e);

                             oRpt = new Customer();

                             GenerateReport();

                   }

                   

                   /// <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);

 

                   }

                   #endregion

 

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

                   {

                             GenerateReport();

                   }

                   protected void GenerateReport()

                   {

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

                                                                      catalog=Northwind;");

                                                         

                             SqlCommand comd;

                             comd = new SqlCommand();

                             comd.Connection = sqlConn;

                             comd.CommandType = CommandType.StoredProcedure;

                             comd.CommandText = "up_GetAllCustomer";

 

                             comd.Parameters.Add("@Companyname",SqlDbType.VarChar,50);

                             if(TextBox1.Text.Trim()!="")

                                      comd.Parameters[0].Value=TextBox1.Text;

                             else

                                      comd.Parameters[0].Value=DBNull.Value;

         

                             SqlDataAdapter sqlAdapter = new SqlDataAdapter();

                             sqlAdapter.SelectCommand = comd;

                            

                             Dataset1 ds = new Dataset1();

                             sqlAdapter.Fill(ds, "Customers");

                            

                             oRpt.SetDataSource (ds);

                                               

                             CrystalReportViewer1.Visible=true;

                   

                             CrystalReportViewer1.ReportSource = oRpt;

                   }

          }

}

 

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

 

Enjoy Coding. 

Login to add your contents and source code to this article
Article Extensions
Contents added by Kiran Ch on Mar 13, 2012
Hi All,

I've Found simplest way to generate Crystal reports dynamically in ASP.NET using C#.net.

For that I've prepared  DataSet dynamically.

To Illustrate clearly I am giving some screen shots.


I hope this will help you.
If you want source code write your query @kiranmca.du@gmail.com

Thank you...

-
kiran
@kiranmca.du@gmail.com


Contents added by asha rani on Aug 17, 2011
Contents added by prajwal on Jun 08, 2010
Contents added by christo pher on May 17, 2009
This is for Crystal Reports Using Dataset
share this article :
post comment
 

i have some confusion plz help me out what is customer in the following line? "public Customer oRpt = null;" it looks like a class but you have not mentioned anything about "Customer"

Posted by Nitin Singh Chaurasia Mar 07, 2012

nice article mr. roy

Posted by bhagwat sharma Jan 20, 2012

Hi Author, Great article !!!!!!!!! Thanks for sharing with us. Here I have also a nice one article, please check out the following links............ http://mindstick.com/Articles/b1fa8372-12c0-4b0e-a870-138fe135a095/?Crystal%20Report%20with%20DataSet Thanks !!!!!!!!!!

Posted by Jaypee Huda Sep 15, 2011

ehere the customer class defined implemented

Posted by lakshmi prem Jul 07, 2011

The Author has faile dto provide the full code.. I have no way of identifying what this oRpt = new Customer(); is all about.. utter rubish!!

Posted by Nirosh Mar 25, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor