Page 278 - DCAP312_WEB_TECHNOLOGIES_II
P. 278
Web Technologies-II
Notes Time-Based Cache Invalidation in a SqlDataSource Control
Caching in ASP.NET is a powerful feature that can increase the performance of a Web
application. In fact, the most dramatic way to improve the performance of a database-driven
Web application is through caching. Retrieving data from a database is one of the slowest Web
site operations that we can perform. However, if we can cache the database data in memory
and avoid accessing the database with every page request, we can dramatically increase the
performance of our application.
ASP.NET 3.5 provides a number of enhancements to the caching feature set in ASP.NET 3.5
One new feature is the ability to specify the caching attributes as part of the data source control
declarations. The new data source controls in ASP.NET 3.5 work seamlessly with the new caching
features of ASP.NET 3.5, enabling us to set the caching attributes as part of the SqlDataSource
control declaration.
We can set the following two properties in the SqlDataSource control to enable caching:
Enable Caching: By setting this attribute to true, we enable caching in a SqlDataSource control.
Cache Duration: This property allows us to set or get the duration of the cached data in the
SqlDataSource control. This attribute is specified in terms of seconds.
For the purposes of this example, consider a categories and products table in the Northwind
database. It displays all the categories in a DropDownList and the products that belong to a
specific category in a GridView control. Start by creating a new Web site named Caching in
Visual Studio 2005. Next, add a Web page named TimeBasedCaching.aspx to it. Modify the
code in the TimeBasedCaching.aspx file to look like the following:
<%@ Page Language= “C#” %>
<html>
<head>
<title>SqlDataSource Control Caching And Parameters</title>
</head>
<body>
<form id= “form1” runat= “server”>
<asp:DropDownList DataValueField= “CategoryID”
DataTextField= “CategoryName”
DataSourceID= “CategoriesDataSource”
ID= “DropDownList1” Runat= “server” AutoPostBack= “True”>
</asp:DropDownList>
<br/><br/>
<asp:GridView ID= “GridView1” Runat= “server”
DataSourceID= “ProductsDataSource”
DataKeyNames= “ProductID” AutoGenerateColumns= “False”>
<Columns>
<asp:BoundField HeaderText= “ProductID” DataField= “ProductID”
SortExpression= “ProductID”/>
<asp:BoundField HeaderText= “Timestamp” DataField= “Timestamp”
< SortExpression= “Timestamp”/>
<asp:BoundField HeaderText= “ProductName”
272 LOVELY PROFESSIONAL UNIVERSITY