banner



How To Create Crystal Report In Visual Studio 2008

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

2. Adding DataTable to the Typed DataSet

Our next step would be to add a DataTable to the Type DataSet.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

As soon as you click OK you get the following dialog. You must select Using the Report Wizard option.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

Once you add the CrystalReportViewer control to the Form, your Form must look as below.

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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

Basic Crystal Report Tutorial with example in Windows Forms (WinForms) Application using C# and VB.Net

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

How To Create Crystal Report In Visual Studio 2008

Source: https://www.aspsnippets.com/Articles/Basic-Crystal-Report-Tutorial-with-example-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx

Posted by: clarklects1948.blogspot.com

0 Response to "How To Create Crystal Report In Visual Studio 2008"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel