Crystal Report In Windows Application Without Database Connection

Crystal Report is a Reporting Tool for creating various reports. It provides a platform to create reports and graphs with user friendly design and also helps to connect with databases. This reporting tool has the facility to produce reports with connecting databases as well as without connecting databases.

Here I will explain how to create a Crystal Report without a database connection.

I have the following windows application,

 

In this windows form design when I press the book button after entering data it will directly go to the DataGrid and will print a good report after the "print" button is clicked; this is my requirement. So here I will show you the steps to Create this.

Here is the code to bind the data after clicking Book button without database.

Add a dataset and datatable where form loads
  1. public partial class LunchDinnerBookingEntry : Form  
  2.    {  
  3.          
  4.         static DataSet1 ds = new DataSet1();  
  5.        DataTable dt = new DataTable();  
  6.          
Creating a method as Createrow. 
  1. public void createnewrow()  
  2.       {  
  3.           if(dt.Rows.Count<=0)  
  4.           {  
  5.   
  6.               DataColumn dc1 = new DataColumn("PERSONAL NO"typeof(int));  
  7.               DataColumn dc2 = new DataColumn("NAME"typeof(string));  
  8.               DataColumn dc3 = new DataColumn("DATE"typeof(string));  
  9.               DataColumn dc4 = new DataColumn("QUANTITY"typeof(int));  
  10.               DataColumn dc5 = new DataColumn("TYPE"typeof(string));  
  11.   
  12.   
  13.               dt.Columns.Add(dc1);  
  14.               dt.Columns.Add(dc2);  
  15.               dt.Columns.Add(dc3);  
  16.               dt.Columns.Add(dc4);  
  17.               dt.Columns.Add(dc5);  
  18.   
  19.   
  20.   
  21.               dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());  
  22.   
  23.   
  24.               dataGridView1.DataSource = dt;  
  25.               ds.Tables.Add(dt);  
  26.   
  27.           }  
  28.           else  
  29.           {  
  30.   
  31.               dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());  
  32.   
  33.   
  34.               dataGridView1.DataSource = dt;  
  35.                 
  36.   
  37.           }  
  38.        }  
Call the method on Book button click.
  1. private void btn_book_Click(object sender, EventArgs e)  
  2.       {  
  3.             
  4.           createnewrow();  
  5.           
  6.       }  
Here are the steps to add DataSet and Crystal Report.

STEP 1: 
Right click on Solution Explorer and select Add New Item and choose a Dataset.

  

STEP 2: 
After adding dataset it will show the following form.

 

STEP 3: 
Right click on Dataset Designer and add new Datatable.

 

STEP 4: 
Add the Columns for the datatable as follows.

 

STEP 5: 
Now adding dataset is over. Now the second task is to add Crystal Report in the project.Right click on the project, Add New  Item, then Add Crystal Report.

 

STEP 6: 
Crystal Report type as standard.

 

STEP 7: 
Select the dataset you created above and its datatable.

 

Now select the datatable to Right side.

 

Click Next and Expand the datatable.

 

STEP 8: Add a new form Print and add a CrystalReporter Viewer in this.

 
When clicking on Print button write the code to show this "Print" form.
  1. private void button1_Click(object sender, EventArgs e)  
  2.        {  
  3.            Print obj = new Print();  
  4.            
  5.            obj.Show();  
  6.        }  
Now go to the Print Form and double click on Crystal Report Viewer and write the following code. 
  1. private void crystalReportViewer1_Load(object sender, EventArgs e)  
  2.        {  
  3.            Print p = new Print();  
  4.            
  5.            Token rpt = new Token();  
  6.            LunchDinnerBookingEntry LBE = new LunchDinnerBookingEntry();  
  7.            DataSet ds=new DataSet();  
  8.   
  9.            ds = LBE.returndata();  
  10.            rpt.SetDataSource(ds.Tables["table1"]);  
  11.            this.crystalReportViewer1.ReportSource = rpt;  
  12.            crystalReportViewer1.Refresh();  
  13.        }  
NOTE:

Here the data will come from "LunchDinnerBookingEntry"  to "Print" form so i am creating a method in "LunchDinnerBookingEntry" form which will return me the dataset which is bound to the datagrid. And I have called this from CrystalReportViewer as shown in green color.
Here is the Returndata method.
  1. public DataSet returndata()  
  2.         {  
  3.             return ds;  
  4.         }  
As the dataset is declared globally so will take all the data we have entered. Now design the report header and footer.
Now run the application.

 

Now click the print button to print the report.

 

I have designed the report with header and footer of the report and it will print the report as shown above.
 
Read more articles on Crystal Reports:

Up Next
    Ebook Download
    View all
    Learn
    View all