.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Chat Application Using Application State Object

Posted By:BangaruBabuPureti       Posted Date: June 30, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article, I will explain small chatting example by using Application Object, I will also discuss the various events in Global.asax file

In this article, I will explain small chatting example by using Application Object, I will also discuss the various events in Global.asax file.

Application State:

Application State is one of the Server Side State management Mechanism which stores application memory on the server rather than persisting data in the client side memory.If we declare any application variable in a single project, it will be accessible along the website. It will optimize the performance of the website rather than storing the data in database.Application variables save the memory by creating instance to HttpApplicationState class. We can use that variable in all pages throughout the project.Memory of Application Variable expires until we close the Web Project from the web server.


//Defining App Variables -In Global.asax file

 DataSet ds=new DataSet ();

 Application ["KeyVal"] =ds;

 //Using App Variables

 DataSet ds= (DataSet) (Application ["KeyVal"]);

 Gridview1.DataSource=ds.Tables [0];

 Gridview1.DataBound ();

 Global Application Class [Global.asax]

Application state only achieved by adding Global.asax file into our project, it not mandatory to define Global.asax file web project, if you have not added Global.asax file, ASP.NET Runtime detects that there was no Application Statemangement in the current project.

 Events in Global.asax File

Some of the Events in Global.asax file are depicted as follows; this is not a complete list of events.

 Application_Start: This event fires executes when the application starts running

 Application_End: This event ocurs on application shutdown

 Application_Error: This event ocurs when unhandled error occurs

 Session_Start: This event ocurs when new session is started.

 Session_End: This event occurs when session ends

Hands on Chat Application:

This is very small application, just like kids play so there is no need to prolong my explanation on this case study by explaining each and every control and all.


Design web form as follows by using three textboxes and one image button. The size of the window was managed by using java script window.open () method. I created two pages one is Defalut.aspx and another one is chat.aspx. On form load event of Default.aspx, I opened chat.aspx as a popup window.

 Defalut.aspx (Source Code)

 <script language="JavaScript" type ="text/javascript">

function popup()


var newWin = window.open("Chat.aspx", 'method_desc', 'width=250,height=500,left=350,top=120')



<body onload="popup(); return false;">

</body >

 Chat.aspx (Page Design)


Add Global.asax file in you web application and manage add the code as follows

 <script runat="server">

 void Application_Start(object sender, EventArgs e)


        // Code that runs on application startup


        Application["msg"] = "";



  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.




 Step: 3

protected void Page_Load(object sender, EventArgs e)


        //Assigning Application Object to string Varaiable by Parsing

        string msg = (string)Application["msg"];

        txtchat.Text  = msg;


 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)


       string nickname = txtnickname.Text;

       string chatmessage = txtmessage.Text;

       string mymessage = nickname +"::" + chatmessage;

       //Appending the User Entered Data To Application["msg"] object

       Application["msg"] = Application["msg"] + mymessage + Environment.NewLine; 

      //Finally Dispaling on the Chat Content

      txtchat.Text = Application["msg"].ToString();

      txtmessage.Text = "";



Run the application in Browser and deploy it on your local IIS or access the URI in two browser and Check it will maintain the State each use session and finally my content was as follows

Here I completed only global chat without taking any session expiration and all. This is not a Full round trip for Private chat even; this is just for understanding the Application State of a web application.


I hope this article will give brief idea on how to use Application Server State by Using Application Object. Application state variable expires after Stopping Web Server. So in order to maintain the State Permanently we will go for Customized Caching to persist the state of the Server.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend