This article is about creating Crystal Reports using C#.NET. About two weeks ago, one of my friends asked me for this kind of example. He also searched through the Internet and said that he couldn't find a sample. When I was doing this kind of development, I also looked for samples, but I couldn't find one. That’s why I wrote this article on The Code Project without creating reports separately using Crystal Reports other than using .NET IDE.
Create a Dataset
First you should create a Dataset to get data from the DB. By clicking the “Add New Item” in the “Project” menu, you can add a Dataset. A picture of an added Dataset looks like the following window and you can add elements to the Dataset by dragging and dropping elements from the toolbox. This Dataset is used to fill the data, which it gets from the DB by using a query. Because of that, the names of the elements in this Dataset should be equal to the names of the elements in the DB. For an example, this Dataset can fill using the following query.
String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=..\\..\\myDB.mdb;User ID=Admin;Password=";
OleDbConnection conn = new OleDbConnection(connString);
string query = "SELECT studentID, firstName, lastName, birthDate, _
address, contactNo FROM studentInfo";
OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);
Create a Report using Crystal Reports
Now that we have a created Dataset, we can use it to fill the report with data, which we will be getting from the DB. As I mentioned before, you can add a Crystal Report to the project by clicking “Add New Item” in the “Project” menu. Then the following window will appear, and you can select your choices and click OK.
The next window looks like the following and you have to select your created Dataset under “Project Data”, and click “Insert Table”, then click next.
Then you have to add the field, which you want to display in the report through the following window and click next.
You can also go to other tabs of this window and select/deselect your choices. Use the last tab “Style” to select the format of the report. You can also type a Report Title here and click finish.
Then your report creation is done by the .NET IDE. If you want to make any changes to the report, you can do so by using the .NET IDE.
Set the Created Report to Display in the Form
Then you have to set a “
crystalReportViewer” in your form to load the report that you created earlier. Moreover, you need to set the report source of this “
crystalReportViewer” component, which falls in the properties panel or you can set the report source by using the code like the following:
DBConnection DBConn = new DBConnection();
OleDbDataAdapter myDataAdapter = DBConn.getDataFromDB();
DataSet dataReport = new DataSet();
myDataReport myDataReport = new myDataReport();
crystalReportViewer1.ReportSource = myDataReport;
You can also create reports using Crystal Reports separately without using the .NET IDE. For that, you have to install Crystal Reports as well, and you should save those reports in a directory. Then you have to set the report source of the “
crystalReportViewer” component to the particular report under your report Directory. For example:
crystalReportViewer1.ReportSource = @“..\Reports\salesReport.rpt”;
But a better way is the previous one, because we can get data from the DB according to the inputs, which are done by the user of the Applications.
Regarding this article, you can send any questions to email@example.com. I will send you any information that you want for your development.
- 13th January, 2006: Initial post