Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Performance - Using Application & Session Objects to Store Data in VB
Using Application & Session Objects to Store Data in VB


ASP.NET Performance Tutorial

This tutorial shows how we can store small amounts of data in the Application and Session objects - like the number of users on the site. VB Version.


Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

We're going to use the Application and Session objects to keep count of how many users are visiting our site.
First, we build our page which is going to use variables stored in the Application and Session objects. It might look something like this:

<form id="form1" runat="server">
<div>
<asp:Label ID="labelUserCount" runat="server"></asp:Label><br />
Please enter your name:
<asp:TextBox ID="textBoxName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="buttonSubmit1" runat="server" OnClick="buttonSubmit1_Click" Text="Button" /><br />
<asp:Label ID="labelOutput" runat="server"></asp:Label>
</div>
</form>

The Global.asax is where we access the Application and Session objects to initialize our variables when they both start. The Application object exists as long as the website is online; the Session object exists as long as there is a user visiting the site - every unique visitor gets their own unique Session ID.
Upon Application Start, we add a variable to the object, and set it to zero. Upon Session Start, we increment that variable by one. This will give us an idea of how many current visitors there are, because each user has their own Session. And finally, upon Session End, we decrement the same variable by one - indicating that a user left the site.
The Global.asax file should look something like:

<%@ Application Language="vb" %>

<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Add("userCount", 0)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Dim userCount As Integer = Integer.Parse(Application.Get("userCount").ToString())
userCount += 1

Application.Set("userCount", userCount)
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
Dim userCount As Integer = Integer.Parse(Application.Get("userCount").ToString())
userCount -= 1

Application.Set("userCount", userCount)
End Sub

</script>

The code-behind will look something like this:

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.SetFocus(textBoxName)
labelUserCount.Text = "Number of users visiting: " & Application.Get("userCount").ToString()
End Sub
Protected Sub buttonSubmit1_Click(ByVal sender As Object, ByVal e As EventArgs)
labelOutput.Text = "Your name is: " & textBoxName.Text
textBoxName.Text = ""
End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks