The Database Object Model
The ReportDocument class, shown in Figure 17-1, manages the connections to your report’s data sources. It gives you runtime access to the methods and properties to dynamically manipulate and optimize the connections.
Figure 17-1. The Database class diagram.
This diagram shows the ReportDocument class and the two primary properties that work with data sources. The first is the Database property. It is a collection of tables and table links. Each table has properties that set the relevant information for connecting to the table as well as the fields in each table. This is the class that this chapter focuses on. The second property of the ReportDocument class is the DataDefinition class. It has the read-only collection of SQL expression fields.
This section only applies to reports that use the Pull Model. The Pull Model saves connection information with the report and you have to modify the connection properties to set the user credentials. This doesn’t apply if you are using the Push Model because you have to connect to the data source manually. The report doesn’t save any of the connection information and consequently there is nothing for you to change. The Push Model is discussed later in this chapter.
Logging on to a secure data source uses two classes: Table and ConnectionInfo. The Table class is the primary class. The report object stores each table that it uses within a Tables collection. Loop through the Tables collection to reference each Table object. The Name property identifies which database table it is.
Within the Table object is the TableLogOnInfo object. This is only used to get a reference to the ConnectionInfo object. The ConnectionInfo object has the properties UserID and Password. The UserID property is already set to the user ID that you entered during design mode. If this is what you want, you do not have to change it. However, you probably want to reassign it with the current user’s credentials. The Password property is always empty. You have to assign it with the current user’s password.
When the UserID and Password properties are set, call the ApplyLogOnInfo() method of the Table object. After this is done for each table, Crystal Reports attempts to log on to the data source when it prints. If the information is valid, the user is logged in and the report prints. If the information isn’t valid, a login dialog box is displayed so that new user information can be entered.
If you want to find out if the user credentials are valid before opening the report, call the TestConnectivity() method of the Table object. It returns True if there was a successful login.
Setting the logon properties is done differently depending upon whether you are doing so with the ReportDocument object or if you are using the viewer control. Although both use the same objects and properties, the way you get to this information is different. This is explained in detail in the next two sections.