Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.22 Changing the Data Source

Programmatically Changing the Data Source

Some programs require changing the data source of a table while the application is running. Although this isn’t common, it does happen. A common scenario is an application that wants to use the same report for multiple data sources. For example, a sales application could offer the user two reports: a sales report that shows current year data and a sales report that shows historical data. By changing the data source during run-time, you can design a single report that handles both options.

You can only change the data source when using the Pull Method. The Pull Method stores the data source information you set at design time and this is what is used by default. The nature of the Push Method is that no data source information is saved with the report. It only knows the XML schema for a table. Thus, you always have to manually set the data source information every time you run the report.

The two classes that were used earlier in the chapter to modify logon information are the same classes used to change a table’s data source: Table and ConnectionInfo. But this time, rather than modify the UserId and Password properties, modify the ServerName and DatabaseName properties.

The ServerName and DatabaseName properties represent different values depending upon which data source you are connecting to. Table 17-3 shows what belongs in each property.

Table 17-3. The properties for each type of data source.

Data Source ServerName DatabaseName
MS Access The fully qualified file path Leave it blank
SQL Server The server name The database name
ODBC Driver The DSN name Leave it blank

The data source isn’t designed to have its connection type changed. For example, if the report is connected to SQL Server with an ODBC connection, then you can’t modify the connection during runtime to use OLE DB or any other type of connection. This would require the report engine to change the DLL it uses to process the data source. This is set when the application is compiled and can’t be changed.

The data source can only be changed using the ReportDocument object. You can’t use the viewer control to modify the data source during runtime.