Share via


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.

  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).

    myDataSet = DataSetConfiguration.CustomerDataSet
    
    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.

``` vb
If Cache("customerDataSet") Is Nothing Then
    myDataSet = DataSetConfiguration.CustomerDataSet
    Cache("customerDataSet") = myDataSet
Else
    myDataSet = CType(Cache("customerDataSet"), DataSet)
End If
```

``` csharp
if (Cache["customerDataSet"] == null)
{
    dataSet = DataSetConfiguration.CustomerDataSet;
    Cache["customerDataSet"] = dataSet;
}
else
{
    dataSet = (DataSet)Cache["customerDataSet"];
}
```
  1. To test the caching of the DataSet, select Start from the Debug menu.

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

  2. Click your browser's Refresh button.

The Customer report displays again quickly, because the DataSet is now retrieved from the ASP.NET Cache object.
  1. Return to Visual Studio and click Stop to exit from debug mode.