How To Create Crystal Reports

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

How To: Create a Crystal Report from ADO.

NET Dataset using


Visual Basic .NET

Introduction:

Crystal Report of Visual Studio .NET is the standard reporting tool for Visual Studio .NET. You can host
reports on web and windows platform and can publish reports as Report Web services on the web server.
It is based on framework of Crystal Report 8.0 and uses open and flexible architecture, with standards like
XML, to allow porting reports over the web. Using crystal report expert you can choose report layouts,
display charts, calculate summaries, subtotals as grouped data as well as conditionally format text and
rotate text objects.

Although Crystal Reports for Visual Studio .NET supports variety of data source like ADO recordset, CDO
recordset, DAO recordset, MS Excel workbook, this walkthrough endeavor to explain How to report off
ADO.NET DataSet using Visual Basic .NET. As you all know DataSet is the core component of distributed
application and is explicitly designed for data access independent of any data source. Dataset can be
created from variety of sources. Whatever the source is, before reporting off ADO.NET DataSet you must
perform the following task:

 Generate an object for the Dataset.


 Connect report to Dataset Object.
 Push data into Dataset Object.
 Bind report to Windows Forms Viewer to display report with actual data at runtime.

Generating an Object for the Dataset

Object for ADO.NET is a collection of dataset classes created in memory.

To create a dataset object from a database in SQL Server, using ADO.NET Dataset Designer:

1. In the Solution Explorer, right-click the project name, point to Add, and click Add New Item.
2. In the Templates area, select Dataset.
3. Accept the default name Dataset1.xsd. This creates a new schema file that will be used to
generate a strongly typed dataset. The schema file will be displayed in ADO.NET Dataset
designer.
4. In the Solutions Explorer, click on Dataset1.xsd file, if now already the active view.
5. From the Server Explorer, on the right connect to SQL Server and drill down to the database
which you want to get connected.
6. Highlight the Table or stored procedure and drag and drop it on the Interface of Dataset1.xsd.
Dataset1.xsd should now be displayed in the Dataset tab. This creates a dataset object and
contains only a description of the database based on the schema in Dataset1.xsd. It does not
contain the actual data.
This creates a dataset object and contains only a description of the database based on the
schema in Dataset1.xsd. It does not contain the actual data.

Connecting Report to an ADO.NET Dataset Object

From ADO.NET Dataset Object you can add tables to Crystal Report using Database Expert in
Crystal Report Designer.

To create a new report and connect it to Dataset object which contains description for
Customers table in Northwind database

1. In the Visual Studio .NET Solution Explorer, right-click your project to display the
shortcut menu.
2. Point to Add and click Add New Item.
3. In the Add New Item dialog box, select Crystal Report from the Templates area.
4. Click Open.
5. Crystal Report Gallery will be displayed.
6. You can choose from any of the options provided in Crystal Report Gallery. But for the
purpose of this walkthrough choose Using the Report Expert and click OK.
7. The Standard Report Creation Wizard opens.
8. Expand Project Data.
9. Then expand ADO.NET Datasets.
10. There you will find the previously added data set Ex: tblItems
11. Select the required data set to right side and click Next.
12. Follow the wizard and select fields and other options to generate the report. Click Finish
once you finish the report creation.
Pushing data into DataSet object and binding report to Windows Forms Viewer

In order to display actual data in the report, you should fill the dataset object with the data before
you bind the report to Windows Forms Viewer. You should do this in the corresponding source file
for Windows Form.

1. Drag and drop CrystalReportViewer control on Form1 and set the DisplayGroupTree property
to False, as shown below

2. Accept the default name as CrystalReportViewer1.


3. Open Form code editor and add the following code on Load event of CrystalReportViewer1.
Imports System.Data.SqlClient

Public Class frmAllItems


Dim ds As New DataSet
Dim da As SqlDataAdapter

Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles CrystalReportViewer1.Load

Dim cmd As SqlCommand

Try
Dim Sql As String = "Select * from tblItems where Itm_Code =’" &_
textbox1.Text & “’”

cmd = New SqlCommand(Sql, con)


Dim adapter As SqlDataAdapter
adapter = New SqlDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "Items")
Dim rpt1 As New crpAllItems()

rpt1.SetDataSource(ds.Tables("Items"))
CrystalReportViewer1.ReportSource = rpt1

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

End Class

You might also like