State management using the Application class in ASP.Net applications
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.
You can have a look at this post , this post , this post and this one.
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 assume that you have access to a version of SQL Server and Northwind database.
If you do not, you can download and install the free SQL Server Express edition from here. If you need the installation scripts for the sample Northwind database, click here .
1) Launch Visual Studio 2005,2008/2010. Express editions will work fine. I am using Visual Studio 2010 Ultimate edition.
2) Create an empty asp.net web site. Choose an appropriate name. Choose C# as the development language.
3) Add an item to your website, a web form.Leave the default name.Drag and drop a Gridview web server control on the form.
4) Add a new item to your website, a global applicatio class, Global.asax.
5) In the web.config add a new connection string that points to the database.In my case it looks like this,<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=USER-PC\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
6) In the Application_Start event of the Global.asax typeDataTable MyCategories = new DataTable();
string conn= ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].
ConnectionString;SqlDataAdapter adapter = new SqlDataAdapter("SELECT CategoryName, Description from Categories", conn);
Application["categories"] = MyCategories;
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!!!!