ARTICLE

Session and Application in ASP.NET

Posted by Vineet Kumar Saini Articles | ASP.NET Programming November 01, 2011
This article shows the use of session and application level variables in ASP.NET.
Reader Level:
Download Files:
 

Introduction

Session and Application are very important in ASP.NET. When we move from one page to another page, the values of the previous page will get lost, If we want to hold the previous values, for that purpose we can use session level variable or application level variables.

Session level variable : Value will persist till the close of the browser.

Example : First of all we will take a button and text box from the toolbox on web form like the below image.

image1.gif

then we will add code on the button click.

using
System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace session
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = TextBox1.Text;
            Response.Redirect("WebForm1.aspx"); 
        }
    }
}

Now we will add a new web form (WebForm1.aspx)

Solution Explorer->Right click->add->new item->Web Form->add.

image2.gif

image7.gif

Now create a label on this web form, then add the following code on web form loading.

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace session
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Most welcome Mr./Mrs--" + Session["username"].ToString();  
        }
    }
}

Now run the application (Press F5). Then enter the value in textbox.

image3.gif

Then click the ok button.

image4.gif

Application Level variable : Value will persist till the down of web server.

Global.asax file : Global.asax file is ASP.NET application file. Global.asax is extension of Global Application Class. Global.asax file resides in the IIS virtual root of an ASP.NET application. It is used to declare application level variable. There are many events in Global.asax file which are as follows:

Application_Init: Fires when the application initializes for the first time.

Application_Start: Fires the first time an application starts.

Session_Start: Fires the first time when a user's session is started.

Application_BeginRequest: Fires each time a new request comes in.

Application_EndRequest: Fires when the request ends.

Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.

Application_Error: Fires when an unhandled error occurs within the application.

Session_End: Fires whenever a single user Session ends or times out.

Application_End: Fires when the application ends or times out.

References : "http://en.wikipedia.org/wiki/Global.asax"

Example : HIT counter

Solution Explorer->Right click->add->new item->Global application class (Global.asax)->add likes below image.

image2.gif

image8.gif

Then coding on Global.asax file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace Application
{    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["hits"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            Application["hits"] = Int32.Parse(Application["hits"].ToString()) + 1;
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }
        protected void Application_Error(object sender, EventArgs e)
        {
        }
        protected void Session_End(object sender, EventArgs e)
        {
        }
        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}

Now we will take a button on web form then coding on button click.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Application
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("<h1>Your are user Number-->" + Application["hits"].ToString());   
        }
    }
}

Now run application (Press F5). Then click the button.

image5.gif

Again run application then click button.

image6.gif

So when we run the application again and again the number of views will be increment according to running mode of web application.

Summary

So session level variable or application level variable are used for the persist value till the close of browser or server.

Login to add your contents and source code to this article
post comment
     

Can we assign value in Applicaton["hits"] from .cs file insted of Global.asax file

Posted by Santosh Yadav Mar 16, 2013

very good post...

Posted by Santosh Yadav Mar 16, 2013

When you deploy as asp.net web app into a web farm environment, each web servers machine.config or web.config must specify the same key used for encrypting the view state. Remember the view state is encrypted for security reasons and each machine.config on each web server will have a different key so they must all be the same. Best way is to add a machineKey element into each of the web server's web.config and define the same keys and algorithm. The machineKey goes under the System.web node.

Posted by Vineet Kumar Saini Nov 07, 2011

If web application hosted on web farm than, how it is going to work. As far as I know it stores on server's memory, and in the web farm there application will be hosted on multiple servers to balance the load.

Posted by Nitya Sharma Nov 03, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts