Sunteți pe pagina 1din 20

C# Crystal Reports step by step

A step by step tutorial for beginners who is creating their Crystal Reports for the first time in C#. All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb Here we are going to create a new Crystal Reports in C# from Product table in the above mentioned database crystalDB. The Product Table has three fields (Product_id,Product_name,Product_price) and we are showing the whole data from Product table to the C# - Crystal Reports project. Open Visual Studio .NET and select a new CSharp Windows project.

Now you will get the default Form1.cs. From the main menu in Visual Studio C# project select PROJECT-->Add New Item . Then Add New Item dialogue will appear and select Crystal Reports from the dialogue box.

Select Report type from Crystal Reports gallery.

Accept the default settings and click OK. Next step is to select the appropriate connection to your database (here crstaldb). Here we are going to select OLEDB Connection for SQL Server to connect Crystal Reports in C#. Select OLE DB (ADO) from Create New Connection .

Select Microsoft OLE DB Provider for SQL Server .

The next screen is the SQL Server authentication screen for connecting to the database crystalDB. Select your Sql Server name , enter userid , password and select your Database Name .

Click next , Then the screen shows OLE DB Property values , leave it as it is , and then click finish button.

After you click the finish button , the next window you will get your Server name under OLEDB Connection, from there selected database name (Crystaldb) and click the tables , then you can see all your tables from your database. From the tables list double click the Product table then you can see the Product table will come in the right side list.

Click Next Button Select all fields from Product table to the right side list .

Click Finish Button. Then you can see the Crystal Reports designer window in your C# project. In the Crystal Reports designer window you can see the selected fields from Product table. You can arrange the field Objects and design of the screen according your requirements. After that your screen is look like the following picture.

Now the designing part is over and the next step is to call the Crystal Reports in your C# application and view it through Crystal Reports Viewer control in C#. Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

After you drag the CrystalReportViewer to your form , it will look like the following picture.

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code. using CrystalDecisions.CrystalReports.Engine; Copy and paste the following source code and run your C# project Download Source Code Print Source Code Cloud Applications without Coding Royalty Free and Excel Compatible Generate database and reporting .NET Web SpreadsheetGear for .NET apps in minutes. Quickly create visually Featuring the fastest and most complete Excel stunning, feature-rich apps that are easy to compatible calculation engine - Download a customize and ready to deploy. Download Now!free 30-day evaluation today!
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)

{ ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }

NOTES: cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); The Crystal Reports file path in your C# project file location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt When you run the source code you will get the report like the following picture.

When you click the button, the application will ask the username and password. Later in this tutorial you can find how to avoid asking username and password - C# Dynamic logon parameters in Crystal Reports. Hope this tutorial help you to create your first Crystal Reports in C#.

Introduction
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. Thats 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.
Collapse | Copy Code String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_ Data Source=..\\..\\myDB.mdb;User ID=Admin;Password="; OleDbConnection conn = new OleDbConnection(connString); conn.Open(); 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:
Collapse | Copy Code // code to get data from the DB DBConnection DBConn = new DBConnection(); OleDbDataAdapter myDataAdapter = DBConn.getDataFromDB(); // use the created Dataset to fill it with data got // from the DB by using the DataAdapter DataSet dataReport = new DataSet(); myDataAdapter.Fill(dataReport,"myPersonalInfoTable"); // create a new report from the created CrystalReport

myDataReport myDataReport = new myDataReport(); // set the data source of the report myDataReport.SetDataSource(dataReport); // set the report source of the created crystalReportViewer // component to the created report crystalReportViewer1.ReportSource = myDataReport;

Additional Information
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:
Collapse | Copy Code 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 isharasjc@gmail.com. I will send you any information that you want for your development.

S-ar putea să vă placă și