How to Programmatically Load a Report
You won't normally load reports from the My Documents Visual Studio Projects folder on an end user's computer, and the login information and database location are normally different when you deploy a report. For this reason, you must be able to load a report dynamically and set login information at runtime.
Using the TableLogOnInfo class of the CrystalDecisions.Shared namespace, you have programmatic access to the login information for the database tables in a report. Using the ReportDocument class, you can specify a report to load at runtime.
In your applications, you should have a single form named rptViewer that has the report viewer control. The report name that the user is requesting is passed in the constructor for the frmViewer. It's a good idea to have a folder named Reports in your solution where all the reports are located, so you can use the relative path of the report to load at runtime.
The code in Listing 18.1 can be added to either the Load event of the frmViewer or after the InitializeComponent call in the form's constructor. After calling the Load method on the ReportDocument object, you set the ConnectionInfo properties by looping through the Tables collection that exists in the report. To tell the viewer what to load, you simply set the ReportSource property just as you did in the properties window for the viewer.
Listing 18.1. Loading a Crystal Report in Code
Private Sub rptViewer_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Cursor = Cursors.WaitCursor
Try
Dim oRpt As New ReportDocument()
oRpt.Load("..Reports" & strReportName)
Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table
Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo
For Each tbCurrent In oRpt.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
With tliCurrent.ConnectionInfo
.ServerName = "jb1gs"
.UserID = "sa"
.Password = ""
.DatabaseName = "Northwind"
End With
tbCurrent.ApplyLogOnInfo(tliCurrent)
Next tbCurrent
viewer.ReportSource = oRpt
Catch ex As Exception
MsgBox(ex.Message)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
private void frmViewer_Load(object sender, System.EventArgs e)
{
CrystalReport1 oRpt = new CrystalReport1 ();
Database oDb;
Tables oTables;
Table oTable;
TableLogOnInfo tliCurrent;
ConnectionInfo oCnInfo = new ConnectionInfo ();
// Setup the connection information structure to be used
// to log onto the datasource for the report.
oCnInfo.ServerName = "jb1gs";
oCnInfo.DatabaseName = "Northwind";
oCnInfo.UserID = "sa";
oCnInfo.Password = "admin";
//Get the table information from the report
oDb = oRpt.Database;
oTables = oDb.Tables;
//Loop through all tables in the report and
// apply the connection
//information for each table.
for (int i = 0; i < oTables.Count; i++)
{
oTable = oTables [i];
tliCurrent = oTable.LogOnInfo;
tliCurrent.ConnectionInfo = oCnInfo;
oTable.ApplyLogOnInfo(tliCurrent);
}
crystalReportViewer1.ReportSource = oRpt;
}
|
After the ConnectionInfo properties and the ReportSource are set, the report will display in the viewer.