Crystal Report in C# Windows Application (Complete Guide)

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_productas 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 –

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s