What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7509 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Chat Application Using Application State Object

Chat Application Using Application State Object

4 vote(s)
Rating: 4.5 out of 5
Article posted by BangaruBabu on 6/25/2010 | Views: 8413 | Category: ASP.NET | Level: Beginner red flag


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


 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.



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://bangarubabupureti.spaces.live.com
Member since:Wednesday, May 12, 2010
Level:Starter
Status: [Member]
Biography:Microsoft Technology Enthusiast
 Responses
Posted by: Abhi2434 | Posted on: 26 Jun 2010 11:24:22 AM

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 | Posted on: 27 Jun 2010 01:44:23 AM

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 | Posted on: 15 Sep 2011 09:27:07 AM | 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.

>> Write Response - Respond to this post and get points
Related Posts

ASP.NET 4.0, came up with two new properties inside Page Class, those are MetaDescription and MetaKeyWord. This has been introduce because of make web application Search Engine Friendly. Search Engine looks for Meta tag of our web page to get the details of page contents. In ASP.NET 4.0, we can add these two properties with page class in Code behind or in Page Directives.

It is very frequntly asked question in any of the forum or question-answer section on websites. So I decided to write a very compact article with source code. It is as simple as 123.

In this article we are going to see the fileupload without postback.. We are going to do this by using ICallbackEventHandler..

I will show you how to implement Password Recovery control with CAPTCHA included and how to handle the background processing.

In this example i'll show how to detect the session timeout which occurs when user is idle for the time specified as Session.Timeout,using C# asp.NET and if it is than redirect the user to login page to login again, for this i've set time out value in web.config file to 1 minute

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/26/2013 4:32:54 AM