Database Credentials
One of the nice things about the version 10 Crystal Report Viewers as opposed to previous versions is that if the report needs database credentials, it prompts the user for this information. Figures 29.6 and 29.7 show the Windows Forms and Web Forms Viewers database credential prompting.
Figure 29.6. The Windows Forms Viewer prompts for database credentials.
Figure 29.7. The Web Forms Viewer prompts for database credentials.
Although this is a nice feature, you will often want to suppress this and handle the database credentials themselves. The first reason to do this is to change the appearance or behavior of the database credential process. This could be as simple as customizing the look and feel of the user interface or perhaps changing the behavior in some way. For example, you could have the user prompted the first time but offer to save the credentials for later. This could be accomplished by writing the credentials to a cookie or database. The second reason for suppressing the viewers prompting would be to set the credentials transparently behind the scenes, so the user won need to enter them at all. The logic of the viewers is to determine whether credentials have been supplied through the viewer directly, and if not, to see if the corresponding report has them defined, and finally if not, to prompt the user. Therefore the solution to customizing or eliminating the database credential prompts is to simply set them before the report is viewed.
The easiest way to do this is to use the ReportDocuments SetDatabaseLogon method. This function is overloaded for several different argument types. There are really only two of them that you will use. The simplest version of SetDatabaseLogon accepts two strings: a username and password. Keep in mind that Crystal Reports stores the information required to connect to the database inside the RPT file, so unless you want to change the database, you only need to set the username and password. An example of this is shown in the following code:
Dim Report As New ReportDocument() Report.Load("C:ReportsFinance.rpt") Report.SetDatabaseLogon("username", "password") Viewer.ReportSource = Report
In this case, the Viewer object could be either a Windows Forms Viewer or a Web Forms Viewer because they both have the ReportSource property.
NOTE
When using the Web Forms Viewer, keep in mind that the viewer is stateless, that is, each time the ASPX page is processed, the credentials need to be set unless you are caching the ReportDocument object somewhere.
The other version of the SetDatabaseLogon method takes four string arguments: username, password, server name, and database name. This is useful for taking reports based off a test database and pointing them to a production database. Simply pass in the server name and database name you want the report to use, like this:
Report.SetDatabaseLogon("username", "password", "SERVER01", "SalesDB")
Категории