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:
- 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.STEPSRight 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.
- Generate Dataset from the Dataset XSD.
STEPSRight click on the dataset xsd pane and click Generate Dataset
- Create Crystal Report.
STEPSRight Click Solution Explorer -> Add -> Add New Item -> choose Crystal Report under the Categories (Web Project Items).
- Configure the Crystal Report.
STEPS
- Select Report Layout, ProjectData, ADO.NET DataSets
- Expand ADO.NET DataSets and select the table
- Select the fields
- Create a WebForm and drag and drop the CrystalReportViewer control from the Toolbox(General).
- Put a textbox and button
- 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.
- //
- 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=;initialcatalog=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.
Dinesh GabhanePosted Nov 12, 2019, 6:44 AM
Good Article
Saadi KhanPosted Jan 15, 2014, 2:50 PM
Please follow the below link to know more about creating crystal reports using Data Sets http://www.thesstech.com/how-create-crystal-report-visual-studio-2010-using-data-sets
Saadi KhanPosted Jan 15, 2014, 2:49 PM
Nice work.
Bill GatesPosted Jan 22, 2013, 11:33 PM
Thank you Subhajit Datta Roy & Kiran Ch, very helpful article & screenshots..
Bill GatesPosted Jan 22, 2013, 11:33 PM
Thank you Subhajit Datta Roy & Kiran Ch, very helpful article & screenshots..
Bill GatesPosted Jan 22, 2013, 11:33 PM
Thank you Subhajit Datta Roy & Kiran Ch, very helpful article & screenshots..
Bill GatesPosted Jan 22, 2013, 11:33 PM
Thank you Subhajit Datta Roy & Kiran Ch, very helpful article & screenshots..
Sayful IslamPosted Nov 22, 2012, 2:16 AM
Hi Kiran, thanks for your link with screenshots. Its really helpful.
Pratibha LadiPosted Aug 20, 2012, 2:39 AM
i want multiple tables result in data set
Nitin Singh ChaurasiaPosted Mar 7, 2012, 2:27 AM
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"
bhagwat sharmaPosted Jan 20, 2012, 4:04 AM
nice article mr. roy
Jaypee HudaPosted Sep 15, 2011, 12:22 PM
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 !!!!!!!!!!
lakshmi premPosted Jul 7, 2011, 1:14 AM
ehere the customer class defined implemented
NiroshPosted Mar 25, 2011, 1:39 AM
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!!
nguyenlong tranhuuPosted Jan 7, 2011, 7:18 AM
I don't look class Customer and DataSet1 when download file .zip above. you application will run which is Visual Studio? Can you send for me to mail [email protected]? Is ok? I need urgent. Thanks so much
pushpendra palPosted Nov 26, 2010, 5:20 AM
thanx sir.
PMBPosted Sep 16, 2010, 7:24 AM
I developed a Inventory mgt project in c# (vs-2010, sqlserver 2008). I want to create a report (not crystal report) in my project from tables connected via keys (PK,FK). e.g. one of the table named Group_Master has two field (1. Gr_Id(PK), 2. Gr_Name) an another table Part_Master has fields like Part_Id, Gr_Id(FK), Part_Name. Now I want to create report from second table i.e. Part_Master, but it should show Gr_Name in place of Gr_Id. How it is possible.
bhupal bPosted May 31, 2010, 1:34 AM
i am trying to do this example i am getting an error as 'data source has no tables 'at oRpt.SetDataSource (ds); this line.here i am using empty report. So can u please tell me how to use dataset to this empty report. please post answer please please it is important i canot find answer to this error in google. Thanks in advance
deepak kumarPosted Apr 29, 2010, 1:52 AM
hi all i have a problem i want to prepare report , in that report data should be add dynamically... is there any solution then please tell me or mail me on [email protected]. thanks and regards Deepak Kumar
bala krishnaPosted Mar 15, 2010, 7:17 AM
i am not getting the report, how to assign the data to report, i am getting data from database
subhash choudharyPosted Mar 7, 2010, 4:31 PM
thanks to help me...
jPosted Feb 15, 2010, 12:38 AM
I have code similar to this and the report is not getting the data from the dataset but rather the entire underlying database table that I used to design the report. Is there a property of the report viewer or report to tell it to get data from my dataset and not the dataabse? Thanks.
Jason JIANGPosted Nov 25, 2009, 7:24 PM
My boss want me use crystal report to report the information on our website, so i come here to learn. Thanks for this resource!
venkateshkumar pPosted Aug 10, 2009, 2:23 AM
Hi i am new for .net. i dont know how to make a crystal report with dataset. Any one known about that pls help me
neelima tripathiPosted Jul 6, 2009, 2:39 AM
Hi subhajit.. m new in .net 3.5 as i was facing the problem to make crystal report in asp.net 2008, and your artilce was quite helping , but i still did'nt understand step 2 and step 4 where u wrote "generate dataset" what is the use of this line while we again creating dataset in code file.. in step 4 . will u plz clear it..here is my id [email protected]
Mithilesh SupkarPosted Jun 29, 2009, 1:25 AM
Hi I am unable to generate the dataset from the XSD file. Actually i am not geting "dataset XSD pane". I am a novice user. Please help me.
rohini goudPosted Jun 12, 2009, 2:43 AM
Hi, actually i want to take a empty crystal report and then want to send the data from the dataset. actually i have a function which will give me a query so i binded that query in to the table dynamicaly now i want to take that table and fill the details in the crystal report. can you pls help me out in this?
indira devieditedPosted Mar 20, 2008, 5:09 AMEdited Mar 20, 2008, 5:58 AM
hello sir this is my coding .if any modifications pls tell me. CrystalReport2 obj = new CrystalReport2(); OdbcConnection con = new OdbcConnection(str); OdbcDataAdapter da1 = new OdbcDataAdapter("SELECT Reserve_Group_Crm.Sno,Reserve_Group_Crm.Group_Name,Room_Reserve.Room_Name,Room_Reserve.Room_No, min(Room_Reserve.Reserve_Date_From) FROM staff.dbo.Reserve_Group_Crm Reserve_Group_Crm INNER JOIN staff.dbo.Room_Reserve Room_Reserve ON Reserve_Group_Crm.Sno=Room_Reserve.SerialNo where reserve_group_crm.sno=2 group by Reserve_Group_Crm.Sno, Reserve_Group_Crm.Group_Name, Room_Reserve.Room_Name,Room_Reserve.Room_No", con); DataTable dt = new DataTable(); ; da1.Fill(dt); DataSet ds = new DataSet(); ds.Tables.Add(dt); obj.SetDatabaseLogon("sa", "master"); obj.SetDataSource(ds); obj.PrintToPrinter(1, false, 0, 0); in my form print button is there.if i click print button,i want report with print. if u know answer,pls send this mailid. [email protected] or this coding CrystalReport2 obj = new CrystalReport2(); OdbcConnection con = new OdbcConnection(str); OdbcDataAdapter da = new OdbcDataAdapter("SELECT distinct(Reserve_Group_Crm.Sno),Payment_Details1.Payee_Name,Reserve_Group_Crm.Group_Name,Room_Reserve.Room_Name,Room_Reserve.Room_No FROM (staff.dbo.Reserve_Group_Crm Reserve_Group_Crm INNER JOIN staff.dbo.Room_Reserve Room_Reserve ON Reserve_Group_Crm.Sno=Room_Reserve.SerialNo) INNER JOIN staff.dbo.Payment_Details1 Payment_Details1 ON Room_Reserve.SerialNo=Payment_Details1.SerialNo where reserve_group_crm.sno=2", con); DataSet ds = new DataSet(); da.Fill(ds, "emp"); obj.SetDatabaseLogon("sa", "master"); obj.SetDataSource(ds.Tables["emp"]); obj.PrintToPrinter(1, false, 0, 0);
indira deviPosted Mar 20, 2008, 4:38 AM
how to assign dataset to crystal report object using c#.means in datasset particular conditions is there. if u know answer pls send this id. [email protected]
Nrn KapoorPosted Jan 21, 2008, 4:45 PM
I saw your article on CR and have a question if you can help. using crystal report in VS 2005 C#. I want to print multiple copies(3) of a same report in a single page. Is is possible. Can you please help me? Thanks Neetu
sambath rajPosted Dec 12, 2007, 7:03 AM
Hi Ray,I am using .net2005 i am not possible to call the Crystal report name in code behind file(.vb or .cs)like public Customer oRpt. There is any other way.
vipin goyalPosted Sep 11, 2007, 8:06 AM
i want to upload crystal reports on server but facing problem in web config file
mohammed alkaffPosted Nov 2, 2006, 4:43 AM
Hello How Can I Passe The Parameter of .Net To crystalReport that Use The Store Procedure, The StoreProcedure has Parameters thanks
puneet aggarwalPosted Jul 30, 2006, 4:02 AM
i m creating a data set at runtime and i dont knwo how many column it contains before runtime...... i m able to view the contents of dataset using datagrid... but i m not able to view the contents using crystal reports... please tell me how to do that??
Addison PanPosted Mar 16, 2006, 9:54 PM
Could you explain what Customer is in your example. I do not see where it is defined in the Webform1.aspx or the .cs or the .resx. public Customer oRpt = null;