Tuesday, 19 July 2011

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;

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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;
}
}
}

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The flow for the code behind page is as follows.

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!

No comments:

Post a Comment