Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12943 |  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: 8512 | Category: ASP.NET | Level: Beginner red flag

Advertisements

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



Advertisements

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

Mostly time Developers do not use Editable grid-view because of lot’s of reason… Developers prefer manually coding but editable gridview is good way to update database. I am using Gridview by Wizard do Update. I am submitting all process to step by step first fill then create bool function and go to assemble row editing & updating Event.

This article will help you in getting a brief overlook about the commonly used Alt + Shortcut keys

To do paginations to display large number of records on the page using GridView and Display a custom message when no records to display in the GridView, we can follow this approach.

This article covers on how drop down list controls are dynamically binded to grid view control.

If we could have accessed the Bind method ,that implicitly performs two-way binding, in the code-behind then we could have just override the RowDatabound event and assigned the value to the gridview's checkbox control using the Bind method. But unfortunately the Bind is not available in ASP.Net 2.0 instead Eval only is available. We could still achieve this in two simple and easy steps. 1. Wrap the Eval(fieldname) HTML code in a function eg: Checkvalue that checks the datafield value, performs transformations to True OR False values and assigns to Chekced property. 2. Override the RowUpdating event of gridview and update only the Checked property's value since the SQLDataSource that is bound to gridview will take care of remaining fields to update. Please let me know if this has helped anyone solve the problem!!

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. | 6/18/2013 11:48:06 PM