Use Crystal Reports In C# Windows Application Using Stored Procedure

In this article, I will show you how to use crystal report in windows application using stored procedure using C#.

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, Projects and under Visual C# select Windows.

Windows Application

You can change the name of the project and browse your project to different location too. Then press – OK.

service based database

Step 2: In Solution Explorer you will get your Project, Add Service Based Database. By going to your project right click and Add New Item, then go to Service-based Database.

Database chamber

Step 3: Go to your database Database.mdf, we will create a table - tbl_Data. Go to the database.mdf -Table and add new table. Design your table like the following:

Table - tbl_data (Don’t forget to make ID - Identity Specification - Yes)

Identity Specification

Insert some data inside your table by going to your table - tbl_data, right click and show tbl_data - Add data in the table.

Stored Procedure - sp_select:

sp_select

Double click on the dataset that resides under App_code_Folder. Here's the image:

dataset

When you double click the dataset you get the blank area, there you have to do something like the following:

Right click on the blank area - Add DataTable.

Add Datatable

Datatable

Change the name of table from DataTable1 to Newtbl_data, there you have to add 4 columns for ProductID, ProductName, Quantity, UnitPrice as we had taken in tbl_data, so add three Columns. You will get something like the following image.

Newtbl_data image

After this process you have to go the crystalrpt_demo - Right Click, Add New Item and find Crystal Report.rpt file.

Crystal Report

When you add this a new Crystal Report Gallery opens and  there you have to do the following:

new Crystal Report Gallery

You got something like this:

report

When your crystal report opens like the above image, you can find in the left or right dock, the Field Explorer is visible.

Explorer

Right Click on Field Explorer, Database Expert and create new connection. You have to find the table that we had made in Dataset i.e - Newtbl_data.

Once you got your table, add that table to the right hand side pane using “ >>” button and Press OK. Here's the image:

button

You will get you Newtbl_data in Field Explorer like this.

Newtbl_data

Here you have to add the ProductID, ProductName, Quantity and Unit Price from Database Fields to the Crystal Report - Section 3 Page Details. You can drag these fields to the Crystal Report Page Details.

Crystal Report Page Details

Design chamber

Step 4: Now open your Form1.cs file, where we create our design for Crystal Report Binding
We will drag Crystal Report Viewer from tool box to Form1.cs. Your form will look like the following:

design

Code chamber

Step 5: Open your Form1.cs and write some code so that our application starts working.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using CrystalDecisions.CrystalReports.Engine;  
  11. namespace WindowsFormsApplication5  
  12. {  
  13.     public partial class Form1: Form  
  14.     {  
  15.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  16.         ReportDocument rprt = new ReportDocument();  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         private void Form1_Load(object sender, EventArgs e)  
  22.         {  
  23.             rprt.Load(@"C:\Users\Nilesh\Documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\CrystalReport1.rpt");  
  24.             SqlCommand cmd = new SqlCommand("sp_select", con);  
  25.             cmd.CommandType = CommandType.StoredProcedure;  
  26.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  27.             DataSet ds = new DataSet();  
  28.             sda.Fill(ds, "Newtbl_data");  
  29.             rprt.SetDataSource(ds);  
  30.             crystalReportViewer1.ReportSource = rprt;  
  31.         }  
  32.     }  
  33. }  
Output chamber

Output chamber

Hope you liked this. Thank you for reading. Have a good day.