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.
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)
);
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.
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.
Then select the Item table and then click on the arrow button to place it in the Selected Tables section then click next.
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