Contents

Monday, 12 December 2016

Session Managing Technique(Managing State)


2) Application State

Application State variables are created globally for an application. It works across pages and across different sessions. It is like a multi user global data which is created under root directory. It is created explicitly and the name of this special file is Global.asax. It contains many events which are raised at different events of the application.

Events

Application_Start

Application_Start is an event which is called when the application starts.

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

        }

 Application_End

Application_Error is an event which is called just before the application ends.

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

 Application_Error

Application_Error is an event which is raised for an unhandled exception which we can handle it
here. 

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

        }

The Global,asax file can be added by right clicking on application name>Add>New Item>Global Application Class.

Global.asax code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace httpstateless
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            Application["User Name"]="Akhil Vijayan";
            Application["Blog Name"] = "ASP.NET Tutorial";
        }

        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

        }

        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.


        }

    }
}

In the above Gobal.asax file, we are saving username and blog name in two application object. we now access this in our .aspx page

Application State.aspx Page

Design View



Code View

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace httpstateless
{
    public partial class Application_State : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = Convert.ToString(Application["User Name"]);
            Label2.Text = Convert.ToString(Application["Blog Name"]);
        }
    }
}

You may see that i have converted application variables to string. It is needed because as i said application variables are objects and all the controls in asp.net, hold string values so we need to convert it.

Output