Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.19 Connecting with the Viewer

Logging on with the Viewer Control

Setting the login credentials using the viewer control uses the same objects as the ReportDocument objects. It does so in a slightly different manner because the viewer control doesn’t have any knowledge of the tables in a report. You can’t loop through the Tables collection because the viewer doesn’t have access to it. You have to create the collection yourself and set the properties of each table.

The TableLogOnInfos collection is the viewer’s collection object. It is initially empty and it needs to have a separate Table object for each table in the report. Create a new Table object for each table and set the UserId and Password properties of the ConnectionInfo object. Call the Add() method to add the table to the TableLogOnInfos collection.

Listing 17-12. Setting the Logon credentials with the viewer control.
Public Sub PrintPreview(ByVal UserId As String, ByVal Password As String)
Dim myReport As New CrystalReport1()
Dim myLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
CrystalReportViewer1.LogOnInfo = _
New CrystalDecisions.Shared.TableLogOnInfos()
'Create the Customer table and set its properties
myLogonInfo = New CrystalDecisions.Shared.TableLogOnInfo()
myLogonInfo.TableName = "Customers"
With myLogonInfo.ConnectionInfo
.UserID = UserId
.Password = Password
End With
CrystalReportViewer1.LogOnInfo.Add(myLogonInfo)
'Create the Orders table and set its properties
myLogonInfo = New CrystalDecisions.Shared.TableLogOnInfo()
myLogonInfo.TableName = "Orders"
With myLogonInfo.ConnectionInfo
.UserID = UserId
.Password = Password
End With
CrystalReportViewer1.LogOnInfo.Add(myLogonInfo)
'Show the report
CrystalReportViewer1.ReportSource = myReport
End Sub

The first step is to create a new instance of the TableLogOnInfos() object and assign it to the viewer. This is the collection that stores the table information for the report. The code then creates the Customer table by creating a new instance of a TableLogOnInfo() object and assigning its properties. The three important properties are TableName, UserID, and Password. As mentioned earlier in the chapter, the TableName property is case sensitive and must match the table name in the report exactly. After the properties are set, this object gets added to the TableLogOnInfos collection by calling the Add() method. This is repeated for the Orders table. After all the tables have been added, the report object is assigned to the viewer and the report is displayed.

The choice of names for the viewer properties can be confusing if you aren’t careful. The name of the collection class is called TableLogOnInfos(). Notice that it has an “s” at the end of it. It stores a collection of TableLogOnInfo() objects. Notice that this object name is singular. Now for the confusing part: the viewer’s property name that represents the TableLogOnInfos() collection is called LogOnInfo. Notice that this name doesn’t have the “s” at the end. Thus, the viewer’s collection property doesn’t have the same name as the collection class. Rather it has a similar name as the objects it manages. Be careful or else you might think that the collection is the object it stores.