Tuesday, 19 July 2011

Use Cache with Attachment using ASP.NET 2.0 and VB


This tutorial will show you how to Use Cache using ASP.NET 2.0 and VB.NET.

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

Imports System.Web.Caching;

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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.

Partial Class UseCacheVB
Inherits System.Web.UI.Page

Protected Sub btnLoginBetter_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sKey As String = tbName.Text + "_IsLogin"

Dim sUser As String = Convert.ToString(Cache(sKey))

If sUser = Nothing Or sUser = String.Empty Then
Dim SessTimeOut As New TimeSpan(0, 0, 1, 0, 0)
HttpContext.Current.Cache.Insert(sKey, sKey, Nothing, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, Nothing)

lbUser.Text = "Hello!Welcome"
Else
lbUser.Text = "Sorry,try again in one minutes"
Return
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows.

Imports System.Web.Caching

Partial Class UseCacheVB
Inherits System.Web.UI.Page
Protected Sub btnLoginBetter_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sKey As String = tbName.Text + "_IsLogin"
Dim sUser As String = Convert.ToString(Cache(sKey))

If sUser = Nothing Or sUser = String.Empty Then
Dim SessTimeOut As New TimeSpan(0, 0, 1, 0, 0)
HttpContext.Current.Cache.Insert(sKey, sKey, Nothing, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, Nothing)

lbUser.Text = "Hello!Welcome"
Else
lbUser.Text = "Sorry,try again in one minutes"
Return
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment