Hai My readers,
In this post i will Give a sample program for data caching in asp.net3.5.
Finally i filled all the databound values into the Datalist.
Lets we see in details.
If you are using static data with your ASP.NET applications, such
as that from a database server like Microsoft SQL Server, you should take a
look at caching your data. Previously, this was not an easy thing to do, but the
caching in .NET makes caching your data and objects a trivial task.
In ASP.NET, every page you write extends the
System.Web.UI.Page class, which contains a member called Cache. You use the
Cache property as you would any other IEnumerable, which gives you methods and
properties to get, set, and enumerate through members. We will be looking at
getting and setting items in the cache. In the simplest example, you can
retrieve any object from the cache like so:
Object obj = (Object)Cache["key"];
What this does is look in the Cache for an item with the key "key".
If such an item exists, the object is returned. While we don't need to cast the
returned object to Object, this is just an example of how you must cast the
return to the class type you want returned. If the item is not found, null is
returned, so you will need to do some checking:
Object obj = (Object)Cache["key"];
if (obj == null) {
// Generate a new object and insert it into the cache
}
// Use your object
You can also assign object to the cache like the following code example, but
using Cache.Inset() is a much better means as you'll see later:
Cache["key"] = obj;
Lets We see in details..
Create a Page named Default.aspx and type the coding below..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cache Dependency Tester</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR" />
<meta content="C#" name="CODE_LANGUAGE" />
<meta content="JavaScript" name="vs_defaultClientScript" />
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" />
</head>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataList id="dlUsers" style="Z-INDEX: 101; LEFT: 44px; POSITION: absolute; TOP: 104px" runat="server" Height="148px" Width="343px" BorderWidth="1px" GridLines="Horizontal" CellPadding="4" BackColor="White" ForeColor="Black" BorderStyle="None" BorderColor="#CCCCCC">
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#CC3333"></SelectedItemStyle>
<FooterStyle forecolor="Black" backcolor="#CCCC99"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#333333"></HeaderStyle>
<ItemTemplate>
<table bgcolor="#CCCCFF" border="Black" cellspacing="20">
<tr>
<td><b>User_ID:</b><%#DataBinder.Eval(Container.DataItem,"UserId")%><br />
<b>First_Name:</b><%#DataBinder.Eval(Container.DataItem,"FirstName")%><br />
<b>Last_Name:</b><%#DataBinder.Eval(Container.DataItem,"LastName")%><br /></td>
<td>
</td>
<td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Label id="lblChange" style="Z-INDEX: 102; LEFT: 46px; POSITION: absolute; TOP: 63px" runat="server" Height="28px" Width="295px"></asp:Label>
</form>
</body>
</html>
In that design page i drag and drop one datalist control and added the necessary template.
Goto Default.aspx.cs and type the following Code.
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsUsers;
try
{
if(Cache["Users"]==null)
{
SqlConnection cn;
dsUsers = new DataSet("new");
cn = new SqlConnection("data source=.;Initial Catalog=restaurent;uid=sa;password=1soft;");
// cn = new SqlConnection(ConfigurationSettings.AppSettings.Get("conn"));
SqlDataAdapter daUsers;
daUsers = new SqlDataAdapter("Select * from tblUsers",cn);
cn.Open();
daUsers.Fill(dsUsers,"tblUsers");
//Update the cache object
Cache.Insert("Users",dsUsers, new System.Web.Caching.CacheDependency(
Server.MapPath("Master.xml")), DateTime.Now.AddSeconds(45),TimeSpan.Zero);
HttpContext.Current.Trace.Write(DateTime.Now.AddSeconds(45).ToString() + "is expiry time..");
cn.Close();
cn.Dispose();
HttpContext.Current.Trace.Write("from Database..");
lblChange.Text ="From the database....";
}
else
{
HttpContext.Current.Trace.Write("From cache..");
lblChange.Text ="From the cache....";
dsUsers= (DataSet) Cache["Users"];
}
dlUsers.DataSource =dsUsers;
dlUsers.DataMember = dsUsers.Tables[0].TableName ;
//lblChange.Text += Server.MapPath("Master.xml");
this.DataBind();
}
catch(Exception ex)
{
lblChange.Text = ex.Message;
}
}
Now run the prgram..and u get the desired output.
Happy Coding....
No comments:
Post a Comment