Sunday, October 24, 2021

Generate ASP.NET Crystal Report using DataSet

 This article will give a clear idea of how to create a 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 dragging and dropping 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 a 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  
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Data.SqlClient;  
  12. namespace CrystalReportEg  
  13. {  
  14.     /// <summary>    
  15.     /// Summary description for WebForm1.    
  16.     /// </summary>    
  17.     public class WebForm1 : System.Web.UI.Page  
  18.     {  
  19.         protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;  
  20.         protected System.Web.UI.WebControls.Label Label1;  
  21.         protected System.Web.UI.WebControls.TextBox TextBox1;  
  22.         protected System.Web.UI.WebControls.Button Button1;  
  23.         public Customer oRpt = null;  
  24.         private void Page_Load(object sender, System.EventArgs e)  
  25.         {  
  26.             // Put user code to initialize the page here    
  27.         }  
  28.         #region Web Form Designer generated code    
  29.         override protected void OnInit(EventArgs e)  
  30.         {  
  31.             //    
  32.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.    
  33.             //    
  34.             InitializeComponent();  
  35.             base.OnInit(e);  
  36.             oRpt = new Customer();  
  37.             GenerateReport();  
  38.         }  
  39.         /// <summary>    
  40.         /// Required method for Designer support - do not modify    
  41.         /// the contents of this method with the code editor.    
  42.         /// </summary>    
  43.         private void InitializeComponent()  
  44.         {  
  45.             this.Button1.Click += new System.EventHandler(this.Button1_Click);  
  46.             this.Load += new System.EventHandler(this.Page_Load);  
  47.         }  
  48.         #endregion  
  49.         private void Button1_Click(object sender, System.EventArgs e)  
  50.         {  
  51.             GenerateReport();  
  52.         }  
  53.         protected void GenerateReport()  
  54.         {  
  55.             SqlConnection sqlConn = new SqlConnection("Server=localhost;uid=sa;password=;initialcatalog=Northwind;");  
  56.             SqlCommand comd;  
  57.             comd = new SqlCommand();  
  58.             comd.Connection = sqlConn;  
  59.             comd.CommandType = CommandType.StoredProcedure;  
  60.             comd.CommandText = "up_GetAllCustomer"; comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);  
  61.             if (TextBox1.Text.Trim() != "")  
  62.                 comd.Parameters[0].Value = TextBox1.Text;  
  63.             else  
  64.                 comd.Parameters[0].Value = DBNull.Value;  
  65.             SqlDataAdapter sqlAdapter = new SqlDataAdapter();  
  66.             sqlAdapter.SelectCommand = comd;  
  67.             Dataset1 ds = new Dataset1();  
  68.             sqlAdapter.Fill(ds, "Customers");  
  69.             oRpt.SetDataSource(ds);  
  70.             CrystalReportViewer1.Visible = true;  
  71.             CrystalReportViewer1.ReportSource = oRpt;  
  72.         }  
  73.     }  
  74. }  
Hope this will give you a clear picture of the web crystal report generation.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...