C# tutorial: Entity Framework and Crystal Reports

Generating a report with Entity Framework and Crystal Reports

In the previous page, you learnt how to use Entity Framework and DataGridView to display and manipulate data in a table of SQL Server database. In this tutorial, you will learn to use Crystal Reports tool to generate a report from a table of SQL Server database. Visual Studio 2010 does not include the Crystal Reports tool. However, you can download and install the Crystal Reports tool from here. The table used in this tutorial is Institution. It is in testdb database of SQL Server. This table has two fields: id, and name.

table of sql server database

Now, you need to create a new Windows Forms Application in Visual Studio 2010 and change its target .NET Framework from .NET Framework 4 client profile to .NET Framework 4 (Project->Properties->Application->Target framework). First thing you need to do is adding a connection from SQL Server to the current project. You can add a new connection to the current project by right clicking on Data Connection in Server Explorer (Cltr+Alt+S) and choose Add Connection... from the popup menu. You will see the Add Connection dialog as shown below. In the Server name textbox, enter the name of your SQL Server. Under Log on to the server label, select User SQL Server Authentication radio button and enter your user name and password in to the User name and Password textboxes. Under Connect to a database label, select the first radio button and choose your database from drop down list. Then click Test Connection to confirm the successful connection. Then click OK.

add connection


This connection is required in creating a DataSet object. The DataSet is used in a Crystal Reports template. Now you need to create a DataSet object from the connection created above. Select Project menu->Add New Item...->Data->DataSet.


Then drag and drop the Institution table on to the design area of the Dataset.

add table to dataset

Now, it is the time to add a Crystal Reports tool to the current project. Select Project menu->Add New Item...->Reporting->Crystal Reports. You will see the Crystal Reports Gallery dialog. Click OK.

crystal reports gallery

You will see a dialog that allows you select data for the report. Click to expand Project Data and ADO.NET DataSets. Then select the Institution table and add it to the right list box.

select dataset

Click the Next> button. From the dialog below, select fields of the Institution table and add them to the right list box. Then click Finish button.

select fields

You will get a Crystal Reports template as shown below.

report template

To view the report on the form, you need to drag and drop the CrystalReportViewer control from Toolbox on to the form. Now, you have blank report and a report viewer. To get the data from Institution table of testdb database to put in the report, you need to create a ADO.NET Data Model. If you do not how to create ADO.NET Data Model to get data from SQL Server database, read the Entity Framework page. After ADO.NET Data Model, double-click the Crystal Report View on the form to open its code window. On the crystalReportViewer1_Load procedure write the following code.

testdbEntities db = new testdbEntities();
BindingSource bs = new BindingSource();
bs.DataSource = db.Institutions;
CrystalReport1 rpt=new CrystalReport1();
crystalReportViewer1.ReportSource = rpt;

code to show report

Now you are read to run the program. After pressing F5 to run the program, you get the result as shown below.



Dara comment


You need to install CrystalReports Runtime. You download it from here:

C.A. comment


Hi, thanks for sharing.
I have a question here. What should i install in a client pc besides framework 4 to run sap crystal reports? Do you have any link?


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.