Tuesday, September 18, 2012

what is application state in asp.net

ASP.NET allows you to save values using application state, a global storage mechanism that is accessible from all pages in the Web application. Application state is stored in the Application key/value dictionary. Once you add your application-specific information to application state, the server manages it, and it is never exposed to the client. Application state is a great place to store information that is not user-specific. By storing it in the application state, all pages can access data from a single location in memory, rather than keeping separate copies of the data. Data stored in the Application object is not permanent and is lost any time the application is restarted.
ASP.NET provides three events that enable you to initialize Application variables (free resources when the application shuts down) and respond to Application errors:

a.Application_Start: Raised when the application starts. This is the perfect place to initialize Application variables.

b.Application_End: Raised when an application shuts down. Use this to free application resources and perform logging.

c.Application_Error: Raised when an unhandled error occurs. Use this to perform error logging.

Example :

// This sets the value of the Application Variable



Application["Name"] = txtName.Text; 

Response.Redirect("WebForm5.aspx"); 



// This is how we retrieve the value of the Application Variable



if( Application["Name"] != null ) 

    Label3.Text = Application["Name"].ToString();

No comments: