Crystal Reports XI Release 2  

Caching the DataSet in a Web Site

If you are building a Web Site, you can gain additional scalability and performance by placing your populated DataSet instance into the ASP.NET Cache object. This prevents redundant (and potentially slow) calls to the database to populate the DataSet. For more information, see If You Use DataSets, Cache Them.

Note   If your DataSet contains standard values for all users, use the Cache object. However, if your DataSet contains unique values for each user (based on a user-specific criteria in the WHERE clause of the SQL query, such as user id) then you should use the Session object instead.

In this section, you expand one line of code that assigns the CustomerDataSet property to the DataSet instance to a complete code block that manages DataSet caching.

To modify your Web Site to check for a cached DataSet

  1. Open the Web Form.
  2. From the View menu, click Code.
  3. Locate the line of code where the DataSet instance is assigned to the DataSetConfiguration.CustomerDataSet property (shown below).
    [Visual Basic]
    myDataSet = DataSetConfiguration.CustomerDataSet
    [C#]
    dataSet = DataSetConfiguration.CustomerDataSet;
  4. Replace this line of code with a complete conditional code block that checks for a Cache value named "customerDataSet."

    Enter the conditional block and code exactly as shown here.

    [Visual Basic]
    If Cache("customerDataSet") Is Nothing Then
        myDataSet = DataSetConfiguration.CustomerDataSet
        Cache("customerDataSet") = myDataSet
    Else
        myDataSet = CType(Cache("customerDataSet"), DataSet)
    End If
    [C#]
    if (Cache["customerDataSet"] == null)
    {
        dataSet = DataSetConfiguration.CustomerDataSet;
        Cache["customerDataSet"] = dataSet;
    }
    else
    {
        dataSet = (DataSet)Cache["customerDataSet"];
    }

You are now ready to build and run your Web Site to test the cached DataSet.

To test the caching of the DataSet

  1. From the Build menu, click Build Solution.
  2. If you have any build errors, go ahead and fix them now.
  3. From the Debug menu, click Start.

    The Customer report displays and shows the populated data that you placed in the DataSet.

  4. Click your browser's Refresh button.

    The Customer report displays again quickly, because the DataSet is now retrieved from the ASP.NET Cache object.

  5. Return to Visual Studio and click Stop to exit from debug mode.

Continue to Conclusion.

See Also

Tutorial: Connecting to ADO.NET DataSets | Tutorials and Sample Code | Tutorials' Sample Code Directory