Adding the Report Logon Code

You are now ready to add the logon code to the code-behind class. You begin by creating a private helper method, SetDBLogonForReport().

To create and code the SetDBLogonForReport() method

  1. Return to the code-behind class for this Web or Windows Form.

  2. At the bottom of the class, create a new private method named SetDBLogonForReport() with ConnectionInfo passed into the method signature.

    Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo)
    
    End Sub
    
    private void SetDBLogonForReport(ConnectionInfo connectionInfo)
    {
    }
    
  3. Within this method, retrieve the TableLogOnInfos instance from the LogOnInfo property of the CrystalReportViewer class.

> [!NOTE]
> <P>TableLogOnInfos is an indexed class that contains instances of the TableLogOnInfo class.</P>


``` vb
Dim myTableLogOnInfos As TableLogOnInfos = myCrystalReportViewer.LogOnInfo
```

``` csharp
TableLogOnInfos tableLogOnInfos = crystalReportViewer.LogOnInfo;
```
  1. Create a foreach loop that loops through each TableLogOnInfo instance in the TableLogOnInfos indexed class instance.

    For Each myTableLogOnInfo As TableLogOnInfo In myTableLogOnInfos
    
    Next
    
    foreach(TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
    {
    }
    
  2. Within the foreach loop, set the ConnectionInfo property of TableLogOnInfo to the ConnectionInfo parameter.

    myTableLogOnInfo.ConnectionInfo = myConnectionInfo
    
    tableLogOnInfo.ConnectionInfo = connectionInfo;
    

To modify the ConfigureCrystalReports() method to address the database logon code

This step procedure has created a method to set the logon for the database. However, you must modify the ConfigureCrystalReports() method to address this method, for the report to be aware that it has database logon information.

Modifying the ConfigureCrystalReports() method requires two actions:

  • Configure the ConnectionInfo instance.
  • Call the SetDBLogonForReport() method.
  1. In the ConfigureCrystalReports() method, declare and instantiate the ConnectionInfo class below the code that binds the report to the CrystalReportViewer control.

    Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
    
    ConnectionInfo connectionInfo = new ConnectionInfo();
    
  2. On the next line, call the SetDBLogonForReport() method by passing in the ConnectionInfo instance.

    SetDBLogonForReport(myConnectionInfo)
    
    SetDBLogonForReport(connectionInfo);
    
  3. Set the DatabaseName, UserID, and Password properties of the ConnectionInfo instance.

> [!NOTE]
> <P>For security reasons, it is important that you use a database account with limited access permissions. For more information, see <A href="ms227498(v=vs.90).md">Security: Creating a Limited Access Database Account</A>.</P>


In the code that you write, replace the sample 1234 password (shown below) with your own password.

``` vb
myConnectionInfo.DatabaseName = "Northwind"
myConnectionInfo.UserID = "limitedPermissionAccount"
myConnectionInfo.Password = "1234"
```

``` csharp
connectionInfo.DatabaseName = "Northwind";
connectionInfo.UserID = "limitedPermissionAccount";
connectionInfo.Password = "1234";
```

To test the loading of the NorthwindCustomers report

You are now ready to build and run your project. The report should load properly, because you have added code to log on to the database.

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

The NorthwindCustomers report displays successfully.
  1. Return to Visual Studio and click Stop to exit from debug mode.