Learn Crystal Reports Using C#

Dear reader, this article explains Crystal Reports, another important tool of database programming. This article is very useful for beginners. First of all, we should understand that Crystal Reports is the product of SAP, it's not a component of Microsoft Visual Studio on words to Visual Studio 2010. So we need to install Crystal Reports from the SAP web site http://scn.sap.com/docs/DOC-7824

In this article we will use:

  1. Visual Studio 2010.
  2. Crystal Reports (CRforVS_13_0_3).

Note: we can't install SAP Crystal Reports before installation of Visual Studio 2010.

Step 1: Create the new table in SQL Server.

CREATE TABLE [dbo].[Donor_Profile1](

[Don_Date] [datetime] NULL,

[Don_Donor_ID] [varchar](20) NOT NULL,

[Don_Donor_Name] [varchar](50) NULL,

[Don_Business_Name] [varchar](50) NULL,

[Don_Business_Category] [varchar](10) NULL,

[Don_Market_Name] [varchar](10) NULL,

[Don_Business_Address] [varchar](100) NULL,

[Don_City] [int] NULL,

[Don_District] [int] NULL,

[Don_Business_Phone] [varchar](50) NULL,

[Don_Mobile_Phone] [varchar](50) NULL,

[Don_Email] [varchar](50) NULL,

)


Step 2: Insert a few records in the table.

 

INSERT INTO [psh].[dbo].[Donor_Profile1]([Don_Donor_ID],[Don_Donor_Name],

 [Don_Business_Name],[Don_Business_Category],

[Don_Market_Name],[Don_Business_Address],[Don_City],

[Don_District],[Don_Business_Phone],

[Don_Mobile_Phone],[Don_Email])

VALUES

('CF-00001-12','ARFAH MAJEED','ARFAH FILLING STATION',

'0','0','ARFAH FILLING STATION JHANG.','700','300',

'0000-0000000',   '0332-4491909','arfah@hotmail.com')

 

INSERT INTO [psh].[dbo].[Donor_Profile1]

([Don_Donor_ID],[Don_Donor_Name],

[Don_Business_Name],[Don_Business_Category],

[Don_Market_Name],[Don_Business_Address],[Don_City],

[Don_District],[Don_Business_Phone],

[Don_Mobile_Phone],[Don_Email])

VALUES

('CF-00002-12','NOOR MAJEED','NOOR FILLING STATION',

'0','0','NOOR FILLING STATION JHANG.','700','300',

'0000-0000000',   '0332-4541909','noor@hotmail.com')

 

Step 3: Now we create a Stored Procedure in SQL Server that will help us to design the report. Today we will design the basic report but when we need to design a complex report we need to use the complex query to fetch the data from the server and show on the reports; similarly we can filter the data from the data source. The best practice is to fetch the data from the source using a Stored Procedure as in the following:

Create PROCEDURE [dbo].[Donor_Profile]

AS

BEGIN

SELECT Don_Date, Don_Donor_ID, Don_Donor_Name, Don_Business_Name,

      Don_Business_Category, Don_Market_Name, Don_Business_Address,

      Don_City, Don_District, Don_Business_Phone, Don_Mobile_Phone, Don_Email

FROM Donor_Profile1

END

  

Step 4: Start the new project.


crystal-report-1.jpg


Step 5: Now we need to design the form as shown in the picture.

crystal-report-2.jpg


Step 6: Now we need to design another form as shown in the picture.


Step 7: Now it is time to add the new report to your project.

crystal-report-3.jpg


Step 8: The first screen of Crystal Reports will ask which kind of report to create. We will select the Report Wizard.

crystal-report-4.jpg

 
Step 9: Now we need to create a new data source.

crystal-report-5.jpg


Step 10: First we need to select the type of data source. In our case its SQL Native client.


crystal-report-6.jpg


Step 11: Now we have inserted the user name and password to authenticate the SQL Server. In our case we use the server name: ".\psh" ,user name "sa" and password "###Reno321".


crystal-report-7.jpg


crystal-report-8.jpg

 
Step 12: Now we need to design another form as shown in the following picture:


crystal-report-9.jpg

 
Step 13: Now select the Stored Procedure to be created in SQL server.


crystal-report-10.jpg

 
Step 14: Now select the columns to be in the report.


crystal-report-11.jpg

 
Step 15: Now insert code into the Report_Click event.


crystal-report-13.jpg


Step 16: Now insert the code into the frmShowReport form load event.


crystal-report-14.jpg