Chat Application Using Application State Object

BangaruBabu
Posted by in ASP.NET category on for Beginner level | Views : 18026 red flag
Rating: 4.5 out of 5  
 4 vote(s)

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


 Download source code for Chat Application Using Application State Object

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.

e.g.

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

Step-1:

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')

}

</script>

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

</body >

 Chat.aspx (Page Design)

Step-2

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.Lock();

        Application["msg"] = "";

        Application.UnLock();

  }

  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.

}

      

</script>

 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 = "";

}

Step-4

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.


Conclusion

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.



Page copy protected against web site content infringement by Copyscape

About the Author

BangaruBabu
Full Name: BangaruBabu Pureti
Member Level:
Member Status: Member
Member Since: 5/12/2010 2:15:06 AM
Country: India
BangaruBabu Pureti http://bangarubabupureti.spaces.live.com/
http://bangarubabupureti.spaces.live.com
Microsoft Technology Enthusiast

Login to vote for this post.

Comments or Responses

Posted by: Abhi2434 on: 6/26/2010
I think it means eventually the application object gets bigger and bigger until server crashes.

Why do you use Application to serialize the whole DataSet object ? does it makes sense.

You need to parse the whole dataset object altogether whenever you want to fetch data for a single user.
Posted by: BangaruBabu on: 6/27/2010
Hi Abhi

just I used Data Set parsing only as an Example to show how application declared and used.

We know Already Application Objects Having Some Limitations! Which Stores Session Objects Memory Until Application Stops within application,That Means obviously Some Burden should fall On application.

If you want maintain state of n-users , we need to organize Session State Mode in Sql Server By Using Sql Cache Dependency...

Here i Explained only The Basic Teddy Bear Program By using Application Objects..That's It...





Posted by: Monu1988 on: 9/15/2011 | Points: 25
hi sir Hello
y name is mukesh ,

i m very impress your articles by online cheating .but sir please tell me about the you using the desktop application yes ,
sir your application is connect the online server sof that please sir help me i m develop the different software in desktop application but its not access the online so sir please help me solved my problem to connect the desktop application in online one system to another system like web site .
thaks Sir
this is my ID on fund.com
Monu1988.

Login to post response

Comment using Facebook(Author doesn't get notification)