Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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:

↑ Back to the top


Keywords: KB309018, kbweb, kbstate, kbperformance, kbhowtomaster, kbconfig, kbcaching, kbappdev

↑ Back to the top

Article Info
Article ID : 309018
Revision : 9
Created on : 5/31/2007
Published on : 5/31/2007
Exists online : False
Views : 487