Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
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.
Example 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.
Printing a Report Without the Viewer Control | Next Section

Account Sign In
View your cart