

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
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; |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
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 |
The front end Default.aspx page looks something like this:
<asp:label id="Label1" runat="server">UserName:</asp:label> <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.
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.
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!