Sunday, December 19, 2010

Creating a Crystal Report


Hey All,
            In this series of blog posts I will explain to you on how to use crystal reports in Visual Studio to create your reports.

First up we need to have a database where the crystal report will take data from. You can create the report by connecting directly to the database or by creating a Data Set in your project and adding a Data Table to it and then creating the report by using that Data Table. In this post I will explain on how to create a crystal report by directly connecting to the database.

For all this demos I will create 3 tables in my database and create reports based on them. The 3 tables are Item, Sold and Sold Items. The SQL code is as follows.
 
CREATE TABLE Item(
itemCode INT,
itemName VARCHAR(50),
quantity INT,
price REAL,
CONSTRAINT pk_item PRIMARY KEY (itemCode)
);

CREATE TABLE Sold(
soldID INT,
dateOfSale DATETIME,
CONSTRAINT pk_sold PRIMARY KEY (soldID)
);

CREATE TABLE SoldItems(
soldID INT,
itemCode INT,
quantity INT,
totalPrice REAL,
CONSTRAINT pk_soldItems PRIMARY KEY (soldID,itemCode),
CONSTRAINT fk_soldItems1 FOREIGN KEY (soldID) REFERENCES Sold (soldID),
CONSTRAINT fk_soldItems2 FOREIGN KEY (itemCode) REFERENCES Item (itemCode)
);

So first up create a new windows project in Visual Studio.































I’m using C# as the language. Then select Windows Forms Application and then create the project. Now we have to add a crystal report to our project.

Right click on your project and click on Add --> New Item.



















Then select Reporting and select Crystal Report and then click on OK then a crystal report will be added to the project.














Then you will get a window as shown below.





















Here you get 3 options. You can either use the report creation wizard or create a blank report or choose an existing crystal report. For this demo I will use the wizard. So select the Using the Report Wizard and click on OK.

The next window that is appearing allows you to connect your report with the data.

 















Here it allows you various ways of connecting to the data. Here we will create a New Connection. So expand the Create New Connection.











Since I’m using SQL Server as my database I will be creating an OLE DB (ADO) connection. So expand OLE DB (ADO) in the list then a new window will appear.





















Select SQL Native Client and then click next.





















You have to provide the connection information to your server here. First you have to select the server. If you are using Windows authentication to connect with data SQL server then tick the Integrated Security check box. If you are using SQL Authentication then provide the User ID and Password. Then select the database in where you created the tables then click on Finish. Then a new connection will be made.
 



















Then select the Item table and then click on the arrow button to place it in the Selected Tables section then click next.



















Select the fields that you want to display in your report and then click on the arrow button. Click next then it will ask you what field needs to be grouped. If you don’t want to group it click next. Then it will ask you what fields are to be filtered. If you don’t want to filter then click next. Then it will ask you to select the format of the report.
 

















Here you can select from a range of styles and once you are finished then click on the finish button then your report will get created.














Ok we have created the report now we have to view it. To do so add a Crystal Report Viewer control to Form1 and write the following code in the Form’s Load event.










Now run the application and you will see the data you added to the Item table. As for me I get the following output.


  
 
Now we have created a crystal report. In my next blog post I will explain how to design the crystal report in Visual Studio.Hope you'll will find my blog interesting.




No comments:

Post a Comment