Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.05 Logging On with Viewer Control

Changing the Data Source with the Viewer Control

Changing the data sourcusing the viewer control uses the same classes as the ReportDocument class, but it does so in a slightly different manner. Rather than having a collection object that stores a reference to every table in the report, the viewer stores a reference to each unique database connection. Thus, if a report uses three tables, but they are from the same database, the collection will only have a single object in it. If the report pulls data from two different databases, the viewer will have two objects in the collection. The TableLogOnInfos collection is the viewer’s collection class of database connections. You have to get a reference to each TableLogOnInfo object within this collection and assign the proper connection properties. The viewer doesn’t have an ApplyLogOnInfo() method. You simply overwrite the existing connection info and it takes effect.

Listing 17-3. Setting the Logon credentials with the viewer control.
[VB.NET]
Private Sub LogonToTables(ByVal UserId As String, ByVal Password As String, ByVal ServerName As String, ByVal DatabaseName As String, ByVal ReportFilename As String)
Dim myConnectionInfo As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo()
'Set the database connection info
myConnectionInfo.ServerName = ServerName
myConnectionInfo.DatabaseName = DatabaseName
myConnectionInfo.UserID = UserId
myConnectionInfo.Password = Password
'Apply the connection info to every table
CrystalReportViewer1.ReportSource = ReportFilename
Dim myTableLogOnInfos As CrystalDecisions.Shared.TableLogOnInfos
myTableLogOnInfos = CrystalReportViewer1.LogOnInfo
For Each myTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo In myTableLogOnInfos
myTableLogOnInfo.ConnectionInfo = myConnectionInfo
Next
End Sub
[C#]
private void LogonToTables(string UserId, string Password, string ServerName, string DatabaseName, string ReportFilename)
{
CrystalDecisions.Shared.ConnectionInfo myConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
//Set the database connection info
myConnectionInfo.ServerName = ServerName;
myConnectionInfo.DatabaseName = DatabaseName;
myConnectionInfo.UserID = UserId;
myConnectionInfo.Password = Password;
//Apply the connection info to every table
crystalReportViewer1.ReportSource = ReportFilename;
CrystalDecisions.Shared.TableLogOnInfos myTableLogOnInfos;
myTableLogOnInfos = crystalReportViewer1.LogOnInfo;
foreach (CrystalDecisions.Shared.TableLogOnInfo myTableLogOnInfo in myTableLogOnInfos)
{
myTableLogOnInfo.ConnectionInfo = myConnectionInfo;
}
}

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.