How to use Application state and Global.asax file in ASP.NET Websites

By
Application state:- If we want to store application specific information then we can use Application object.Information is stored in application object that will  accessible to all the user of the application.
      Application state is used to store data corresponding to all the variables of an asp.net application.The data in application state is stored once and read several times.Application state uses the HttpApplication  state class to store and share the data throughout the application .The HttpApplication state class provides a lock method,which you can use to ensure that only one user is able to access and  modify the data of an application at any time.
  The Global.asax Application File:- The Global.asax file resides in the root directory of an asp.net web application.Events and states such as session state and Application state,are specified in the Global.asax file.
     Global.asax file is used to maintain session and  application specific event within an application.We can have only one Global.asax file in a asp.net application. 
There are some steps to implement the application state and Global.asax file in ASP.NET websites ,which are shown below:-

Step 1:- First open your visual studio-->File-->New-->Website-->Select ASP.NET Empty website-->ok-->open solution Explorer--> Add a New web Form -->drag and drop Label,Text Box and Button control on the Form as shown below:-
home page


Step 2:- Double click on submit button -->Write the following codes which are given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   //show the application id value through application state.
        Label1.Text = Application["id"].ToString();
        //count total number of visistor visited the site.
        Label2.Text = Application["count"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application.RemoveAll();
        Response.Redirect("Application.aspx");
    }
}

Step 3:-  Now open solution Explorer--> Add a New web Form(default.aspx)-->drag and drop Label and Button control on the Form (default.aspx) as shown below:-
visited


Step 4:-  Now open your solution Explorer -->Add Global.asax file and write the following code as shown below:-

<%@ Application Language="C#" %>

<script runat="server">
    public static int count = 0;

    void Application_Start(object sender, EventArgs e) 
    {
        Application["count"] = 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) 
    {
        if (Application["count"] != null)
        {
            int i = Convert.ToInt32(Application["count"]) + 1;
            Application["count"] = i;
            Application.UnLock();
        }
        else
        {
            Application["count"] = 1;
        }
    }

    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.
    }      
</script>


Step 5:- Now go Default.aspx file --> Double lick on the form (press F7) -->Write the following codes as shown below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   //show the application id value through application state.
        Label1.Text = Application["id"].ToString();
        //count total number of visistor visited the site.
        Label2.Text = Application["count"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application.RemoveAll();
        Response.Redirect("Application.aspx");
    }
}

Step 6:-Now Run the application(press F5)-->Enter the Required field as shown below:-
required field
Step 7:-  Now click submit button and see the output:-
visitor_count

Note:- This application give error if you run without close it.when run the application after getting the output close the application and run again then you will see,visitor will be  increased. You can handled the error using the Global.asax file  also.
For More:-
  1. Cookie in asp.net website
  2. Validation control in asp.net
  3. web form control
  4. Reflection in c#
  5. Transaction in sql server
  6. Views in Sql server
  7. Create captcha image
For download whole Application
          Download

0 comments:

Post a Comment

Powered by Blogger.