Thursday, November 26, 2009

ASP.Net Application state, difference between application, viewstate & session state

What is ASP.NET Application State?
Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and all sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.
What is Postback?
When an action occurs (like button click), the page containing all the controls within the tag performs an HTTP POST, while having itself as the target URL. This is called Postback.
ASP.NET View State
ASP.NET Web pages provide view state, which is a way for you to store information directly in the page that you want to persist between postbacks.
ASP.NET Cookies
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site. Creating CookiesResponse.Cookies["userName"].Value = "patrick";Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1); HttpCookie aCookie = new HttpCookie("lastVisit");aCookie.Value = DateTime.Now.ToString();aCookie.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(aCookie);
Getting Cookiesif(Request.Cookies["userInfo"] != null){ System.Collections.Specialized.NameValueCollection UserInfoCookieCollection; UserInfoCookieCollection = Request.Cookies["userInfo"].Values; Label1.Text = Server.HtmlEncode(UserInfoCookieCollection["userName"]); Label2.Text = Server.HtmlEncode(UserInfoCookieCollection["lastVisit"]);}

Differences between Session and ViewStates
"Session" is data stored per user session. "Viewstate" is an ASP.NET thing. It's a way for the application to maintain state about a particular form and its child controls. Your ASP.NET application might consist of several pages/forms. There might be a piece of data that you want to access across all pages. You could place that in a Session variable. However, you rarely need to worry about ViewState, ASP.NET manages that itself.

Viewstate variables are stored in the HTML of your page. They are encoded so they are not easily readable, but they are not encrypted. This is generally the best scope to use when you have a variable that is valid for a particular page and you want to store the value between postbacks. Once theuser navigates to another page the value is lost.Session variables are stored on the server (in RAM by default) and are persisted between all pages.

Different between application and session state
The difference is in the length of their existance.An application variable exists from when the application is first 'run' (The first time a page in the app is used) and exists until the application is stopped on the server, either through IIS or the actual machine being turned off or rebooted.A session variable is created for the instance of the browser accessing the site and will only be available until that browser is closed or the session times out.

4 comments:

  1. Different between application and session state
    The difference is in the length of their existance.An application variable exists from when the application is first 'run' (The first time a page in the app is used) and exists until the application is stopped on the server, either through IIS or the actual machine being turned off or rebooted.A session variable is created for the instance of the browser accessing the site and will only be available until that browser is closed or the session times out.

    ReplyDelete
  2. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!
    ri80

    ReplyDelete
  3. very nic n clear...it was of a great help. thanks

    ReplyDelete