Sunday 5 February 2012

Using Application & Session Objects to Store Data in C#

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. C# Version.


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="C#" %>

<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application.Add("userCount", 0);
}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
int userCount = int.Parse(Application.Get("userCount").ToString());
userCount++;

Application.Set("userCount", userCount);
}

void Session_End(object sender, EventArgs e)
{
// 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.
int userCount = int.Parse(Application.Get("userCount").ToString());
userCount--;

Application.Set("userCount", userCount);
}
</script>

The code-behind will look something like this:

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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.SetFocus(textBoxName);
labelUserCount.Text = "Number of users visiting: " + Application.Get("userCount").ToString();
}
protected void buttonSubmit1_Click(object sender, EventArgs e)
{
labelOutput.Text = "Your name is: " + textBoxName.Text;
textBoxName.Text = "";
}

No comments:

Post a Comment