Inserting a DataSet into Cache

Sometimes, we don’t want to hit the database every time we display data, especially when the database doesn’t really change that much. Therefore, to be able to handle this, whenever the page hits the database and creates a DataSet the first time, it can be inserted into Cache, using the Cache API directly.

Create a new page, and call it ‘CacheTest.aspx’. Copy the code below into that page. Notice, that there is a button on the page, below the GridView. What it does, is remove the Cache that had data Inserted into it. That way, you can see that it goes back to read the data directly from the Database, once the Cache is removed. You will see in the text of the label at the bottom of the GridView, just exactly where the page is getting its data (from either the Cache, or directly from the Database).

<%@ Page Language="VB" Trace="True" TraceMode="SortByCategory" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language="VB" Runat="server">
Dim strConn as String
Dim MyConn as SQLConnection
Dim MySQL as String
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
strConn=ConfigurationManager.ConnectionStrings("YourConnString").ConnectionString
MySQL = "Select [EmployeeID], [FirstName], [LastName], [Title], [BirthDate], [email] " & _
"from NWEmployees"
MyConn = New SQLConnection(strConn)
Dim ds as DataSet
ds=CType(Cache("Employees"), DataSet)
if ds Is Nothing then
ds=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"Employees")
Cache.Insert("Employees", ds)
lblCache.text="Reading data Directly"
else
lblCache.text="Reading data from Cache"
End If
gvEmp.Datasource=ds
gvEmp.DataBind()
End If
End Sub

Sub RemoveCache(Source as Object, E as EventArgs)
Cache.Remove("Employees")
response.Redirect("CacheTest.aspx")
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Inserting a DataSet into Cache</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:GridView Runat="server"
Id="gvEmp"
GridLines="Both"
Border-Width="1"
cellpadding="2"
cellspacing="0"
Headerstyle-BackColor="#7988B7"
Headerstyle-Forecolor="#FFFFFF"
Headerstyle-Font-Names="Arial"
Headerstyle-Font-Bold="True"
Headerstyle-Font-Size="12"
BackColor="#E0E0F6"
Font-Names="Arial"
Font-Size="8"
AlternatingRowStyle-BackColor="#EFEFEF"
AlternatingRowStyle-Font-Names="Arial"
AlternatingRowStyle-Font-Size="8"
BorderColor="Black"
AllowPaging = "True"
PageSize = "10"
PagerSettings-Mode = "Numeric">
</asp:GridView>
<asp:Label ID="lblCache" ForeColor="Red" Font-Bold="True" Runat="server" /><br />
<asp:Button id="btnCache" Text="Remove Cache"
onclick="RemoveCache" Runat="server" />
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>