-->

28 January 2020

Asp.Net Application State

  Asp.Net CS By Example       28 January 2020


 Application State 

 In this post we are going to see how to work Application State in ASP.net. If we need to store the variables and object that we want to use throughout the entire web site then Application State we can use. The Application object help us to store and share data between all the pages in asp.net application easily.

Application state is closely similar to Session State but the difference between is that session object store variables and objects for 1 particular for the duration of that user's current visit, whereas application state store objects and variables that are shared between users of application at the same time.

 In ASP.NET application state store data using dictionaries or sets of name-value pairs. We can set the value of application variable named SiteName like this.

C# Code
  Application["SiteName"] = "Intranet Application";

 With SiteName set, any pages in the application can read this string:

C# Code
 string appName = (string)Application["SiteName"]; 

 We can remove an object from application state using the Remove method, like so:

C# Code
 Application.Remove("SiteName"); 

 If we have store multiple objects and variables in application and now we want remove all this from application state then we can use RemoveAll method.

C# Code
 Application.RemoveAll(); 

 It is important to be cautious when using application variables. Objects remain in application state until we remove them using the Remove or RemoveAll methods, or shut down the application in IIS. If we continue to save objects into the application state without removing them, we can place a heavy demand on server resources and dramatically decrease the performance of our applications.

  Code: FrmPageHitCounter.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmPageHitCounter.aspx.cs"
            Inherits="LearnAsp.Net.ControlDemo.ApplicationState.FrmPageHitCounter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>    
    <form id="form1" runat="server">
    <div>
    <b>The page has been requested
    <asp:Label ID="lblCounter" ForeColor="Red" runat="server" />
    times!</b>
    </div>
    </form>
</body>
</html>

  Code: FrmPageHitCounter.aspx.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
namespace LearnAsp.Net.ControlDemo.ApplicationState
{
    public partial class FrmPageHitCounter : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["PageCounter"] != null &&
                (int)Application["PageCounter"] >= 10)
            {
                Application.Remove("PageCounter");
            }
            if (Application["PageCounter"] == null)
            {
                Application["PageCounter"] = 1;
            }
            else
            {
                Application["PageCounter"] =
                (int)Application["PageCounter"] + 1;
            }
            lblCounter.Text = Convert.ToString(Application["PageCounter"]);
        }
    }
}
  Output:
logoblog

Thanks for reading Asp.Net Application State

Previous
« Prev Post

1 comment:

  1. Asp.Net Application State >>>>> Download Now

    >>>>> Download Full

    Asp.Net Application State >>>>> Download LINK

    >>>>> Download Now

    Asp.Net Application State >>>>> Download Full

    >>>>> Download LINK Eq

    ReplyDelete

Please do not enter any spam link in the comment box.