First we have to create a database . Give the database name as "crystaldb"
Create a DataBase "crystaldb"
In the crystaldb database , create three tables
OrderMaster , OrderDetails , Product .
OrderMasterThe following picture shows the relations of each table :
OrderMaster_id
OrderMaster_date
OrderMaster_customer
OrderMaster_createduser
OrderDetails
OrderDetails_id
OrderDetails_masterid
OrderDetails_productid
OrderDetails_qty
Product
Product_id
Product_name
Product_price
In this section we are passing User ID , Password , Server Name and Database Name dynamically to Crystal Reports from vb.net .
SITUATIONS :
In many situations this is useful , for example if you develop a database project in a test server and later you have to migrate it , in such situations you have to give these information dynamically to Crystal Reports.
In this program we are using our earlier program and pass the values dynamically to Crystal Reports. Before we start this section take a look at the How to create Crystal Report in VB.NET .
In the How to create Crystal Report we created a report selecting all data from the Product table . There is no chance to change the server on runtime in that case because it is a static report . Here we are passing Server Name , UserID and Password dynamically to the Crystal Reports.
Here we use Crystal Reports ConnectionInfo .
Dim crConnectionInfo As New ConnectionInfo , and pass these arguments to ConnectionInfo
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 import the following :
Imports CrystalDecisions.CrystalReports.Engine
Imports
CrystalDecisions.Shared
Put the following source code in the button click
event
VB.NET 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
Dim
crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New
TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables
As Tables
Dim CrTable As Table
cryRpt.Load("PUT CRYSTAL REPORT PATH
HERE\CrystalReport1.rpt")
With crConnectionInfo
.ServerName = "YOUR
SERVER NAME"
.DatabaseName = "YOUR DATABASE NAME"
.UserID = "YOUR
DATABASE USERNAME"
.Password = "YOUR DATABASE PASSWORD"
End With
CrTables = cryRpt.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo =
crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class
NOTES: cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here. You have to replace the source code values of "YOUR SERVER NAME" , "YOUR DATABASE NAME" , "YOUR DATABASE USERNAME" , "YOUR DATABASE PASSWORD" with your correct values . When you run this program you will get the following screen.