Data cache example using asp.net?

Sagarp
Posted by Sagarp under ASP.NET category on | Views : 4475
<%@ Page language="c#" Codebehind="Datacache.aspx.cs" AutoEventWireup="false" Inherits="day8.Datacache" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Datacache</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="btInsert" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 72px" runat="server"
Text="Insert" Width="56px"></asp:Button>
<asp:CheckBoxList id="CheckBoxList1" style="Z-INDEX: 109; LEFT: 120px; POSITION: absolute; TOP: 168px"
runat="server"></asp:CheckBoxList>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 264px; POSITION: absolute; TOP: 240px"
runat="server"></asp:DataGrid>
<asp:Button id="Button2" style="Z-INDEX: 107; LEFT: 376px; POSITION: absolute; TOP: 160px" runat="server"
Text="Display"></asp:Button>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 240px; POSITION: absolute; TOP: 160px" runat="server"
Text="InsertCache"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 288px; POSITION: absolute; TOP: 32px" runat="server">Label</asp:Label>
<asp:TextBox id="TextBox2" style="Z-INDEX: 104; LEFT: 232px; POSITION: absolute; TOP: 112px"
runat="server"></asp:TextBox>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
<asp:Button id="btRetrive" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 120px"
runat="server" Text="Retrive"></asp:Button>
</form>
</body>
</HTML>
Datacache.aspx.cs
DataSet ds=new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
adpt=new SqlDataAdapter("select * from emp",con);
ds=returnds();
if(this.IsPostBack==false)
{
this.CheckBoxList1.DataSource=ds.Tables[0].Columns;
this.CheckBoxList1.DataBind();
}


}
private void btInsert_Click(object sender, System.EventArgs e)
{
if(Cache["data"]==null)
{
Cache.Insert("data",this.TextBox1.Text,null,DateTime.MaxValue,TimeSpan.FromSeconds(5));
}
}
public DataSet returnds()
{
adpt.Fill(ds,"emp");
return ds;
}

private void btRetrive_Click(object sender, System.EventArgs e)
{
this.TextBox2.Text=Cache["data"].ToString();
}
DataSet tempds;
private void Button1_Click(object sender, System.EventArgs e)
{
if(Cache["dat"]==null)
{
this.Label1.Text="Insert into cache";
tempds=returnds();
Cache.Insert("dat",tempds,null,DateTime.MaxValue,TimeSpan.FromSeconds(2));

}
}

private void Button2_Click(object sender, System.EventArgs e)
{

try
{
this.Label1.Text="retrive data from cache";
tempds=(DataSet)Cache["dat"];

}
catch(Exception ex)
{
this.Label1.Text=ex.Message;
}

this.DataGrid1.DataSource=tempds.Tables[0];
this.DataGrid1.DataBind();

}

Comments or Responses

Login to post response