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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.