I have explained some of the state mechanisms that we have in our disposal for preserving state in ASP.Net applications in various posts in this blog.
I have not presented yet an example in using the Application class/object for preserving state within our application.
Application state is available globally in an application.The way we access Application State is through the HttpApplication object's Application property.
Let's move on to our hands on example.
I create a new datatable. Then I use the ConfigurationManager class to get the connection string.
I create a new adapter object by passing the Sql statement and the connection string as parameters. Then I fill with data the datatable.
Then I use the Application class to store in memory the datatable.I want to point out that the Application_Start will run the moment the application launches. So it will be available from the very first moments in the lifetime of our application.
7) Now we need to get this datatable data that lives inside the "categories" application object in the memory and bind it to the Gridview
In the Page_Load event handling routine of the default.aspx page type,DataTable categories = (DataTable)Application["categories"];
GridView1.DataSource = categories;
8) We just cast the Application object back to a DataTable object and simply bind it to the GridView control.
In most ASP.Net application, Application state will not be the best solution to do things.In this particular example we could cache the datatable.
I have observed in some ASP.Net applications that use Application state management functionality to have performance and scalability problems.
In the case that have many concurrent users that need to access the data, using Application state management we should use the Lock and Unlock methods of the Application class.
This will degrade performance and as more users want to access our application the performance will not be acceptable.
Despite all that,it is good to know how to use the Application state mechanism.
Hope it helps!!!!