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 –
public partial class Form1 : Form
SqlConnection con = new SqlConnection(@”Data Source=DEVANG\SQLEXPRESS;Initial Catalog=CrystalDemo;User ID=sa;Password=sa123″);
private void Form1_Load(object sender, EventArgs e)
SqlCommand com = new SqlCommand(“Select * From tbl_Product”,con);
SqlDataAdapter sdt = new SqlDataAdapter(com);
DataTable ds =new DataTable();
CrystalReport1 crp = new CrystalReport1();
crystalReportViewer1.ReportSource = crp;
Now, Run the Project and you can see Output as shown below –