In this article I will explain a tutorial with an example, to create a basic Crystal Report in Windows Forms (WinForms) Application using C# and VB.Net.
The Crystal Report will be populated using Typed DataSet in Windows Forms (WinForms) Application using C# and VB.Net.
Note: By default, Visual Studio 2010, 2012 and 2013 does not include Crystal Reports hence you need to download the Crystal Reports 13. Refer my following articles.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
1. Add Typed DataSet to the project
This article makes use of Disconnected Architecture to connect Crystal Reports with Database and hence Typed DataSets will be used.
2. Adding DataTable to the Typed DataSet
Our next step would be to add a DataTable to the Type DataSet.
3. Adding Columns or fields to DataTable
In the DataTable we need to specify the column names that we want to display in the Crystal Report.
Note: The Column Names of the DataTable must exactly match with the actual Database Table column names.
By default all the columns are of String Data Type but you can also change the data type as per your need.
4. Add Crystal Report to the project
Now you will need to add a Crystal Report to the project. You can give it name as per your choice.
As soon as you click OK you get the following dialog. You must select Using the Report Wizard option.
Once you press OK in the above dialog, the Report Wizard starts and you get the following dialog where you need to choose the type of Database connection for your Crystal Report. Since we are using DataSet we will choose the Customers DataSet.
Next the Wizard will ask for the Columns or Fields from the Customer DataSet you need to display on the Crystal Reports. You can choose either all or specific fields as per you choice.
Note: There are more steps in the Wizards but those are Optional hence are not included in this article.
Once you click Finish your Crystal Report should look as below.
5. Adding Crystal Report Viewer to the Form
In order to display the Report, we will need to add CrystalReportViewer control to the Form from the Toolbox.
Once you add the CrystalReportViewer control to the Form, your Form must look as below.
6. Populating the Crystal Report from Database
Inside the Form Load event, first the Customers DataSet is populated with records from the Customers Table.
The Customers DataSet is set as a DataSource for the Crystal Report.
Finally, the Crystal Report is set as ReportSource for the CrystalReportViewer control.
C#
Namespaces
using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
Code
private void Form1_Load(object sender, EventArgs e)
{
CustomerReport crystalReport = new CustomerReport();
Customers dsCustomers = GetData();
crystalReport.SetDataSource(dsCustomers);
this.crystalReportViewer1.ReportSource = crystalReport;
this.crystalReportViewer1.RefreshReport();
}
private Customers GetData()
{
string constr = @"Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
VB.Net
Namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Code
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim crystalReport As New CustomerReport()
Dim dsCustomers As Customers = GetData()
crystalReport.SetDataSource(dsCustomers)
Me.crystalReportViewer1.ReportSource = crystalReport
Me.crystalReportViewer1.RefreshReport()
End Sub
Private Function GetData() As Customers
Dim constr As String = "Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TOP 20 * FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsCustomers As New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Using
End Function
Screenshot
Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.
Downloads