6.1. Maintaining Information Needed by All Users of an Application

Problem

You want to make certain data available to all users of an application.

Solution

Place the code needed to find and load the data in the Application_Start method of global.asax and store it in the Application object.

In the code-behind class for global.asax, use the .NET language of your choice to:

  1. Create an event handler for the Application_Start event.

  2. Load the application data and store it in the Application object.

The code we’ve written to demonstrate this solution is shown in Example 6-1 through Example 6-5. Example 6-1 and Example 6-2 show the VB and C# code-behind files for global.asax; this code reads data from a database and places it in the Application object. Figure 6-1 shows a simple form that we’ve created to view the application state data. Example 6-3 shows the .aspx file that produces the form. Examples Example 6-4 and Example 6-5 show the companion VB and C# code-behind files that demonstrate how to access the application state data.

A view of some sample application state data

Figure 6-1. A view of some sample application state data

Discussion

The purpose of the Application object is to store information once that can be simultaneously shared with all users of the application without having to access it repeatedly from a database or some other data store. A simple example is when you want to store and then share the number of times an application ...

Get ASP.NET Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.