Microsoft KB Archive/309018

From BetaArchive Wiki

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

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

  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.

back to the top

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.

back to the top

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.

back to the top

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.

back to the top

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


Additional query words: application scope variable

Keywords: kbappdev kbcaching kbconfig kbhowtomaster kbperformance kbstate kbweb KB309018