DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Use Cache with Attachment using ASP.NET 2.0 and C#

This tutorial will show you how to Use Cache using ASP.NET 2.0 and C#.NET. This tutorial will show you how to Use Cache using ASP.NET 2.0 and C#.NET. First, you  need to import the System.Web.Caching namespace.
using System.Web.Caching;

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

First, the Cache Calss is Implements the cache for a Web application. This class cannot be inherited. We use the btnLoginBetter_Click event to do the work. We then call  the Class Cache to use Cache.Insert. The methods of Cache.Insert is Overloaded and Inserts an item into the Cache object. Use one of the versions of this method to overwrite an existing Cache item with the same key parameter. After cache login status into memory, we use TimeSpan to destroy login status within one minutes. A TimeSpan object represents a time interval, or duration of time, measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The largest unit of time used to measure duration is a day. Time intervals are measured in days for consistency because the number of days in larger units of time, such as months and years, varies.

public partial class UseCacheCsharp : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnLoginBetter_Click(object sender, EventArgs e)
{
string sKey = tbName.Text + "_IsLogin";
string sUser = Convert.ToString(Cache[sKey]);

if (sUser == null || sUser == String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, 1, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut,
CacheItemPriority.NotRemovable, null);

lbUser.Text = "Hello!Welcome";
}
else
{
lbUser.Text = "Sorry,try again in one minutes";
return;
}
}
}

The front end Default.aspx page looks something like this:

<asp:label id="Label1" runat="server">UserName:</asp:label>&nbsp; &nbsp;
<asp:textbox id="tbName" runat="server" Width="183px"></asp:textbox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbName" ErrorMessage="Please Input UserName!!!"></asp:RequiredFieldValidator><br />
<br />

<asp:label id="Label2" runat="server" Width="78px">PassWord:</asp:label>
<asp:textbox id="tbPass" runat="server" Width="183px"></asp:textbox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbPass" ErrorMessage="Please Input PassWord!!!"></asp:RequiredFieldValidator><br />
<br />

<asp:Button ID="btnLoginBetter" runat="server" OnClick="btnLoginBetter_Click" Text="Login" Width="99px" /><br />
<br />

<asp:Label ID="lbUser" runat="server" Width="286px"></asp:Label>

The flow for the code behind page is as follows.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

public partial class UseCacheCsharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnLoginBetter_Click(object sender, EventArgs e)
{
string sKey = tbName.Text + "_IsLogin";
string sUser = Convert.ToString(Cache[sKey]);
if (sUser == null || sUser == String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, 1, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut,
CacheItemPriority.NotRemovable, null);

lbUser.Text = "Hello!Welcome";
}
else
{
lbUser.Text = "Sorry,try again in one minutes";
return;
}
}
}



Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!