You are on page 1of 28

VB.

NET CRYSTAL REPORTS


Crystal Report is a Reporting application that can generate
reports from various Data Sources . We can Create Reports ,
Print and Print Preview of reports from Crystal Reports .
Crystal Reports are compatible with most popular development
environments like VB.NET etc. and SQL Server also

VB.NET Crystal Reports for Beginners


Start your first VB.NET Crystal Reports .

All Crystal Reports programming samples in this tutorials is


based on the following database (crystaldb) . Please take a look
at the database structure before you start this tutorial - Click
here to seeDatabase Structure .

Open Visual Studio .NET and select a new Visual Basic .NET
Project.

Create a new Crystal Reports for Product table from the above
database crystalDB. The Product Table has three fields
(Product_id,Product_name,Product_price) and we are showing
the whole table data in the Crystal Reports.

From main menu in Visual Studio 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 we are going to select OLEDB connection for
SQL Server

Select OLE DB (ADO) from Create New Connection .


Select Microsoft OLE DB Provider for SQL Server .
Next screen is the SQL Server authentication screen . Select your
Sql Server name , enter userid , password and select
yourDatabase Name . Click next , Then the screen shows OLE
DB Property values , leave it as it is , and click finish.

Then you will get your Server name under OLEDB


Connectionfrom there select database name (Crystaldb) and
click the tables , then you can see all your tables from your
database.

From the tables list select Product table to 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 . You can arrange the design according your
requirements. Your screen look like the following picture.
Now the designing part is over and the next step is to call the
created Crystal Reports in VB.NET through Crystal Reports
Viewer control .

Select the default form (Form1.vb) you created in VB.NET and


drag a button and CrystalReportViewer control to your form.

Select Form's source code view and put the code on top

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click event

Next : VB.NET Crystal Reports from multiple tables

Download Source Code


Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

NOTES:
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Reports is in your project location, there you can


seeCrystalReport1.rpt . So give the full path name of report here.

VB.NET Crystal Reports from multiple


tables
All Crystal Reports programming samples in this tutorials is
based on the following database (crystaldb) .

Here we are going to do is to generate Crystal Reports from


multiple tables. Here we have three table (ordermaster ,
orderdetails amd product ) and we are generating report from
these three tables by connecting each tables with their related
fields.

Open Visual Studio .NET and select a new Visual Basic .NET
Project.

From main menu in Visual Studio 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 we are going to select OLEDB connection for
SQL Server

Select OLE DB (ADO) from Create New Connection .


Select Microsoft OLE DB Provider for SQL Server .
Next screen is the SQL Server authentication screen . Select your
Sql Server name , enter userid , password and select
yourDatabase Name . Click next , Then the screen shows OLE
DB Property values , leave it as it is , and click finish.

Then you will get your Server name under OLEDB


Connectionfrom there select database name (Crystaldb) and
click the tables , then you can see all your tables from your
database.

Select all table from the table list to right side list box, because
we are creating report from three tables ( OrderMaster,
OrderDetails, Product) .
The next step is to make relation between these selected tables.
Here we are connecting the related fields from each table. For
that we arrange the tables in visible area in the list (this is not
necessary ) and select the field we are going to make relation and
drag to the related field of the other table.

VB.NET Crystal Reports String parameter


All Crystal Reports programming samples in this tutorials is
based on the following database (crystaldb) . Please take a look
at the database structure before you start this tutorial - Click
here to seeDatabase Structure .

Here we are passing a String parameter from vb.net to Crystal


Reports . For that , from vb.net program we pass a customer
name as parameter and show all the order of that customer to
the Crystal Reports.

In the previous tutorial we saw how to generate a Crystal Reports


from multiple tables. Here is the continuation of that tutorial , the
only different is that we are passing a Customer Name as a String
parameter and get the report of that particular Customer only .
Before starting to this tutorial just take a look at the previous
tutorial ( Crystal Report from multiple tables ).

In the previous section we are getting the report of all orders


from all customers , that is the all order placed by all customers ,
here is only one customer.

Hope you went through previous tutorial , if not click here


( Crystal Report from multiple tables ).

Next step is to create a string parameter in Crystal report.

Select the Field Explorer from CrystalReport Menu.

Then you can see Field Explorer in the Left hand side.

Select Parameter Field from Field Explorer and right Click.

Fill the appropriate name for Name and Promting text fields
After creating the parameter field , we have to create the
selection formula for the Crystal Reports .

For creating selection formula , Right click on Crystal Reports


designer window , select REPORT -> SELECTION FORMULA ->
RECORD .

Then you can see the record Selection Formula Editor. This for
entering the selection formula. For that you have to select the
fields from above fields and make the formula .

First you have to select OrderMaster.OrderMaster_customername


from Report Field and select the comparison operator and select
the parameter field. Double click each field then it will be
selected.
Form the following picture you can understand the selection
fields.

After the creation of selection formula close that screen .

Now the designing part is over and the next step is to call the
created Crystal Reports in VB.NET through Crystal Reports
Viewer control .

Select the default form (Form1.vb) you created in VB.NET and


drag a Textbox , button and CrystalReportViewer control to
your form.
Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Put the following source code in the button click event

Next : VB.NET Crystal Reports Integer parameter

Download Source Code


Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim cryRpt As New ReportDocument


cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

Dim crParameterFieldDefinitions As ParameterFieldDefinitions


Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue

crParameterDiscreteValue.Value = TextBox1.Text
crParameterFieldDefinitions = -
cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = _
crParameterFieldDefinitions.Item("Customername")
crParameterValues = crParameterFieldDefinition.CurrentValues

crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

VB.NET Crystal Reports Formula Fields


All Crystal Reports programming samples in this tutorials is
based on the following database (crystaldb) . Please take a look
at the database structure before you start this tutorial - Click
here to seeDatabase Structure .
In this tutorial we are adding a Formula Field in existing Crystal
Reports .

SITUATIONS :

If you have a Crystal Reports with Qty and Price , you need an
additional field in your Crystal Reports for the Total of QTY X
PRICE . In this situation you have to use the Formula Field in
Crystal Reports.

In this tutorial we are showing the all orders with qty and price
and the total of each row , that means each in each row we are
showing the total of qty and price. Before starting this tutorial.

Create a new Crystal Reports with fields CustomerName , Order


Date , Product Name and Product Price . If you do not know how
to create this report , just look the previous tutorial Crystal
Report from multiple tables . In that report selecting only four
fields , here we need one more field Prodcut->Price .

After you create the Crystal Reports you screen is look like the
following picture :

Next is to create the a Formula Field for showing the total of


Qty and Price .

Right Click Formula Field in the Field Explorer and click New.
Then you will get an Input Message Box , type Total in textbox
and click Use Editor
Now you can see Formula Editor screen . Now you can enter
which formula you want . Here we want the result of Qty X
Price . For that we select OrderDetails.Qty , the multiplication
operator and Product.Price . Double click each field for selection.

Now you can see Total Under the Formula Field . Drag the field
in to the Crystal Reports where ever you want .
Now the designing part is over . Select the default form
(Form1.vb) you created in VB.NET and drag a button
andCrystalReportViewer control to your form.

Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click event

Next : VB.NET Crystal Reports Summary Fields

Download Source Code


Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

VB.NET Crystal Reports Summary Fields


All Crystal Reports programming samples in this tutorials is
based on the following database (crystaldb) . Please take a look
at the database structure before you start this tutorial - Click
here to seeDatabase Structure .

In this tutorial we are taking the sum of field value of Total . This
is the continuation of the previous tutorial Crystal Report Formula
Field . So before we start this tutorial , take a look at the previous
tutorial Crystal Report Formula Field.

Here we are taking the grand total of the Total field . The Total
field is a Formula field is the result of qty X price .

In the Crystal Reports designer view right click on the Report


Footer , just below the Total field and select Insert -> Summary .

Then you will get a screen , select the Total from the combo box
and Sum from next Combo Box , and summary location Grand
Total (Report Footer) . Click Ok button
Now you can see @Total is just below the Total field in the report
Footer.

Now the designing part is over . Select the default form


(Form1.vb) you created in VB.NET and drag a button
andCrystalReportViewer control to your form.

Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click event

Next : VB.NET Crystal Reports Export to PDF

Download Source Code


Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

Next step is to select the fields from the tables . Here we are
selecting only Customername , orderdate from ordermastertable ,
Productname from product table and quantity from order details.
Click the Finish button because now we are not using other
functionalities of this wizard. After that you will get the Crystal
Reports designer window . You can arrange the fields in the
designer window according to your requirement to view the
report . For rearranging you can drag the field object in the
screen . For editing right click the field object and select Edit Text
Object. The following picture shows the sample of designer
window after rearrange the field.
Now the designing part is over and the next step is to call the
created Crystal Reports in VB.NET through Crystal Reports
Viewer control .

Select the default form (Form1.vb) you created in VB.NET and


drag a button and CrystalReportViewer control to your form.

Select Form's source code view and put the code on top

Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

Crystal Reports from SQL Query String


We can create Crystal Reports in VB.NET using SQL Query String
. Here we create a Strongly Typed dataset for Crystal Reports
design and create a connection object and execute the SQL Query
String .

All Crystal Report programming samples in this tutorials is


based on the following database (crystaldb) . Please take a look
at the database structure before you start this tutorial - Click
here to seeDatabase Structure .

The basics of Crystal Reports creation you can find in the


previous section of this tutorial , before we start this tutorial you
can take a look at the step by step Crystal Report.

Generating a Strongly Typed DataSet

In the previous section you can see a detailed tutorial about


tocreate a strongly typed datset and its Crystal Reports design .
After create the dataset and Crystal Reports design you should
create a connection object and fetch the data from database.

Select the default form (Form1.vb) you created in VB.NET and


drag one button and CrystalReportViewer control to your form.

Put the following source code in the button click events

Next : Dynamic Crystal Reports from SQL Query String

Download Source Code


Print Source Code
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim ds As New DataSet1


Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String

connectionString = "data source=servername; _


initial catalog=crystaldb;user id=username;password=password;"
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT Product_id,Product_name,Product_price FROM Product"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet1
dscmd.Fill(ds, "Product")
MsgBox(ds.Tables(1).Rows.Count)
cnn.Close()

Dim objRpt As New CrystalReport1


objRpt.SetDataSource(ds.Tables(1))
CrystalReportViewer1.ReportSource = objRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

You might also like