Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.18 Connecting with the ReportDocument

Logging On with the ReportDocument

The ReportDocument object has a Tables collection that stores each table in a report. The program loops through each table in this collection and sets the logon properties for it. Listing 17-11 shows how to loop through each data source and set these properties. It sets different login credentials based upon the table.

Listing 17-11. Logging in to multiple tables.
Private Sub LoginToTables(ByVal UserId As String, ByVal Password As String)
Dim myReport As New CrystalReport1()
Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
Dim myLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
'Changing the logon info
For Each myTable In myReport.Database.Tables
myLogonInfo = myTable.LogOnInfo
If myTable.Name = " CorpStandards" Then
With myLogonInfo.ConnectionInfo
.UserID = G_UserId
.Password = G_Password
End With
Else
With myLogonInfo.ConnectionInfo
.UserID = UserId
.Password = Password
End With
End If
myTable.ApplyLogOnInfo(myLogonInfo)
'Test if the user credentials are valid
If Not myTable.TestConnectivity() Then
MessageBox.Show("Invalid user credentials for table: " & myTable.Name)
End If
Next myTable
CrystalReportViewer1.ReportSource = myReport
End Sub

To example illustrates how each table is treated separately. It uses a table called CorpStandards that has public access for everyone in the company. It uses a global user account to login to it. All other tables have specific security and use the current user’s login information. This is managed elsewhere in the program and is passed to this procedure via its parameter list.

The code loops through each table in the ReportDocument.Database. Tables collection and gets a reference to the TableLogOnInfo object. If the table name is “CorpStandards” it applies the generic login information. For all other tables it uses the login parameters passed to the procedure. After setting the properties, it calls the ApplyLogOnInfo() method of the Table class. This updates the table with the new login properties. The TestConnectivity() method is used to determine whether the user credentials are valid or not. If they aren’t then a message box is displayed that tells which table failed. The report viewer is assigned the report document and this forces the report to open the database connections using the user credentials and display the report.