Microsoft KB Archive/309018

= How To Store Data in Application Scope by Using ASP.NET and Visual Basic .NET =

Article ID: 309018

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q309018



For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below to view the article in the Microsoft Knowledge Base:

299683 How To Use the Application Object to Store ASP State

For a Microsoft Visual C# .NET version of this article, see 311515.

IN THIS TASK
SUMMARY
 * Using the Application Object
 * Using the Cache Object
 * Using the AppSettings Object
 * Additional Notes

REFERENCES



SUMMARY
This step-by-step article demonstrates how to store application-wide data through the Application, Cache, and AppSettings objects. Application-wide data is data that is available to the entire Web application.

back to the top

Using the Application Object
 Create a new Visual Basic ASP.NET Web application. Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.  Add the following code to the Page_Load event: TextBox1.Text = Application(&quot;abc&quot;).ToString   Add the following code to the Application_Start event in the code-behind module for the Global.asax file: Application(&quot;abc&quot;) = &quot;hi&quot;  Compile the project. View WebForm1.aspx in your browser.</ol>

back to the top

Using the Cache Object
<ol> Create a new Visual Basic ASP.NET Web application.</li> Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.</li>  Add the following code to the Page_Load event: TextBox1.Text = Cache(&quot;abc&quot;).ToString </li>  Add the following code to the Application_Start event in the code-behind module for the Global.asax file: Context.Cache.Insert (&quot;abc&quot;, &quot;Hello&quot;, Nothing, DateTime.MaxValue, TimeSpan.Zero) NOTE: When you use the Cache object inside the Global.asax file, you must access it through the Context object, such as Context.Cache. </li> Compile the project.</li> View WebForm1.aspx in your browser.</li></ol>

back to the top

Using the AppSettings Object
<ol> Create a new Visual Basic ASP.NET Web application.</li> Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.</li>  Add the following code to the Page_Load event: TextBox1.Text = ConfigurationSettings.AppSettings(&quot;abc&quot;).ToString </li>  In the Web.config file, locate the following section: </li>  Add the following text just below. Note that appSettings is case sensitive. <appSettings> <add key=&quot;abc&quot; value=&quot;Hello World&quot; /> </appSettings> </li> Compile the project.</li> View WebForm1.aspx in your browser.</li></ol>

back to the top

Additional Notes
<ul> If you modify either the Global.asax or the Web.config file (even with Notepad), the Web application is restarted. This purges all data that is stored in memory, such as application and cache data, as well as session data if it is stored in memory (that is, if you are using InProc session state mode).</li> The Cache object has sophisticated memory management features. For demonstration purposes, this article uses DateTime.MaxValue for absolute expiration. Thus, the code does not optimally leverage these memory management features.For additional information the Cache object, click the article number below to view the article in the Microsoft Knowledge Base:

307225 INFO: ASP.NET Caching Overview

</li> How do you know which method is the best for you? Why would you select one method over another?

The ability to modify the value of a variable in code (read/write) while the application is running can be both a benefit and a detriment. This is detrimental if multiple processors (CPUs) or multiple servers are running your Web application. Each CPU or server maintains its own value for the variable. If you change the value of one, your change does not affect any of the other values. <ul> <li>Using the Cache object:

Read/Write - You can add or modify items in code while the application is running. To mimic read-only code, set the variable's value in the Application_Start event and then do not change it anywhere else.

The Cache object becomes more interesting when you use its memory management features. If you have data that needs to be refreshed either regularly or occasionally, this object has many time-based and dependency-based features to invoke a callback function, which you can use to refresh data. Alternately, if some function generates a large amount of data that may (or may not) get reused, the cache can hold it at low priority and purge it if memory is needed for something else.</li> <li>Using the Application object:

Read/Write - You can add or modify items in code while the application is running. To mimic read-only code, set the variable's value in the Application_Start event and then do not change it anywhere else.

Application variables are popular for storing static items that need to be globally accessible and yet modifiable at run time.</li> <li>Using the AppSettings object:

Read only - Items are read in from the Web.config file when the application first starts. You cannot add or modify items without restarting the application.

Similar to Application object variables, AppSettings are most popular for items that remain static and are easier to maintain in one place.</li></ul> </li></ul>

back to the top