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

Article translations Article translations
Article ID: 309018 - View products that this article applies to.
This article was previously published under Q309018
Expand all | Collapse all

On This Page

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.

Using the Application Object

  1. Create a new Visual Basic ASP.NET Web application.
  2. Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
  3. Add the following code to the Page_Load event:
    TextBox1.Text = Application("abc").ToString()
    					
  4. Add the following code to the Application_Start event in the code-behind module for the Global.asax file:
    Application("abc") = "hi"
    					
  5. Compile the project.
  6. View WebForm1.aspx in your browser.

Using the Cache Object

  1. Create a new Visual Basic ASP.NET Web application.
  2. Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
  3. Add the following code to the Page_Load event:
    TextBox1.Text = Cache("abc").ToString()
    					
  4. Add the following code to the Application_Start event in the code-behind module for the Global.asax file:
    Context.Cache.Insert ("abc", "Hello", 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.
  5. Compile the project.
  6. View WebForm1.aspx in your browser.

Using the AppSettings Object

  1. Create a new Visual Basic ASP.NET Web application.
  2. Add a TextBox control to WebForm1.aspx. TextBox1 is created by default.
  3. Add the following code to the Page_Load event:
    TextBox1.Text = ConfigurationSettings.AppSettings("abc").ToString()
    					
  4. In the Web.config file, locate the following section:
    <configuration>
    					
  5. Add the following text just below <configuration>. Note that appSettings is case sensitive.
      <appSettings>
        <add key="abc" value="Hello World" />
      </appSettings>
    					
  6. Compile the project.
  7. View WebForm1.aspx in your browser.

Additional Notes

  • 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).
  • 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
  • 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.
    • 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.
    • 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.
    • 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.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
307626 INFO: ASP.NET Configuration Overview
307598 INFO: ASP.NET State Management Overview
307225 INFO: ASP.NET Caching Overview
For more information, see the following MSDN Web sites:
Application State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconapplicationstate.asp

ASP.NET Caching
http://msdn2.microsoft.com/en-us/library/ms972379.aspx

ASP.NET Caching Features
http://msdn2.microsoft.com/en-us/library/xsbfdd8c(vs.71).aspx

Properties

Article ID: 309018 - Last Review: May 31, 2007 - Revision: 4.5
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
Keywords: 
kbappdev kbcaching kbconfig kbhowtomaster kbperformance kbstate kbweb KB309018

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com