C# Reporting data-CrystalReport
Visual Studio 2008 comes with a useful reporting tool--Crystal Report that enables you to generate reports to show your data retrieved from a database. In this page we will discuss about how to get data stored in a table of a Microsoft SQL SERVER database to display on a form of your project.
Suppose that you want to create a report to display the data from TblProduct of SaleAndStock database. This database exists in Microsoft SQL SERVER(in my machine, i have SQL SEVER 2005). The user that has a right to login to the database is called sa and its password is 123. This login information is useful when we connect the database using C# code. The steps below will help you to get thing done.
-Step 1: Add a DataSet to the project
+Now create a new project in Visual Studio 2008. In my case, i created a project called ReportVB. Ricght-click the project name->Add->New Item. Under Categories, select Data and under Templates select Dataset.
+On the Server Explorer next to the active dataset window, click Connect to Database icon to add a connection to the database server. On the Add Connection dialog, click Change button of the Data source block to show the Change Data Source dialog.
+Then on the Change Data Source dialog, select Microsoft SQL Server. After that you will see a new Add Connection dialog that shows the Server name box. In this box, you need to enter the name of your SQL SERVER. In my case, this name is Dcc-pc.
+In the Log on to the server block, select Use SQL Server Authentication. Then type the user name and password in to the User name box and Password box.
+In the Connection to a database, select Select or enter a database name option. Then click the dropdown list to select SaleAndStock database( a sample database used in this tutorial).
+Click Test Connection button to test the connection to the SQL SERVER. If there is a dialog showing that the Connection is succeed, it means that you can move to the next step. If an error occurs you need to check what you have provided again.
-Step 2: Add the TblProduct table to the dataset
After you connected successfully to the SQL Server database, look Server Explorer again and click Data Connection to expand it then select the connection that you have created in the previous step. Then click Tables to expand and drag the TblProduct table to the dataset designer window.
-Step 3: Add a Crystal Report and link to the table of dataset
Now you have a schema of a dataset that connects to the TblProduct of the SaleAndStock database located in SQL Server. The next step is to add a Crystal Report item in to your project.
+Right-click the project name(ReportVB)->Add->New Item. On the New Item dialog, under Categories select Reporting and under Templates select. You will see the Crystal Reports Gallery dialog. Select As a Blank Report to create a blank report and accept the default name(CrystalReport1).
+To link to the table of the dataset, right-click Field Explorer next to the active window of CrystalReport1 then select Database Expert. You will see the dialog as shown below:
+Select Project Data and select ADO.NET DataSets, then you will see the TblProduct table. Select this table and click the arrow button to add the table to the Selected Table area. Click OK.
+Now back to the active window of the CrystalReport1 and drag fields of TblProduct table that you want to the designer area of the CrystalReport1.
-Step 4: Add a Crystal Report Viewer control to the form of the project( look for this control in the Toolbox) and writing C# code on the form load event.
+Accept the default name of the Crystal Report Viewer(crystalReportVewer1)
+Open the code window by double-click the form and in the Form1_load procedure write the following code:
CrystalReport1 rpt=new CrystalReport1(); //The report you created.
You also need to add the line of code below to the area above the namespace:
Therefore, the full C# code becomes as shown below: