Crystal Report in C# Windows Application (Complete Guide)

5 comments
In this demo, we have to study how to generate crystal report in Windows Application using C#.

In this demo, we have used database called CrystalDemo and one table called tbl_product as shown below -

Step By Step Guide


First add DatSet in your Project (Right click Project Root Folder - > Click Add New Items -> Select DataSet as shown below screen.).


Now add Table Adapter (Right Click in DataSet designer - > Select TableAdapter).



Now choose your Database Connection. If your Database Connection not seen here then create new Connection. Now Click Next.


Now Select Use SQL statement Radio button and click Next.


Now, Enter SQL Statement from What you want to display table as shown in below screenshot. then click Next button.


After Click Finish button, you can see your table shown as below screen.


Now add Crystal Report in your Project. To add Crystal Report Right click Project Root -> Add New Items -> Then select Crystal Report as shown below screen.


Now select Using the Report Wizard radio button and click ok button.


Now, Expand Project Data and select CrystalReportDemo.DataSetProduct from the left penal and move to the right penal as shown below screen. then click Next.


Now, Select all database filed from left panel and move to the right panel as shown below screen. and click Next and then click Finish button.


After Clicking Finish button you can able to see generated crystal report design as shown below. Now we want to Load this crystal report into Windows Form.


Now, Add Windows Form (called "Form1") in your Project and drag and drop the CrystalReportViewer From the toolbox to the Windows Form as Shown Below -


Now add below code to your Windows Form (Form1.cs) as shown below -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace CrystalReportDemo
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=DEVANG\SQLEXPRESS;Initial Catalog=CrystalDemo;User ID=sa;Password=sa123");
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand com = new SqlCommand("Select * From tbl_Product",con);
            com.CommandType= CommandType.Text;
            SqlDataAdapter sdt = new SqlDataAdapter(com);
            DataTable ds =new DataTable();
            sdt.Fill(ds);
            CrystalReport1 crp = new CrystalReport1();
            crp.SetDataSource(ds);
            crystalReportViewer1.ReportSource = crp;
            crystalReportViewer1.Refresh();
            con.Close();
        }
    }
}

Now, Run the Project and you can see Output as shown below -


5 comments:

  1. Following link cover all major issues related to C# Crystal reports development http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm c# crystal reports guide

    ling

    ReplyDelete
  2. More on crystal reports

    http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm

    crystal reports tutorial

    ReplyDelete
  3. when i run my form following ur example then i see this error:
    Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.

    and i face this problem every time when i use crystak report in any widow form..sp kindly help me.

    ReplyDelete
  4. Thanks for information, why we use the dataset when we create the crystal report if we send a sql command on the code? can I send parameters? or it's necessary in all reports? is free crystal reports?
    thanks

    ReplyDelete