7.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 global.asax file, 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 Examples 7-1, 7-2, 7-3, 7-4, 7-5, 7-6 through 7-7. Examples 7-1 and 7-2 show the VB and C# global.asax files; this code reads data from a database and places it in the Application object. Examples 7-3 and 7-4 show the VB and C# classes for constants used throughout the application to access the data in the Application object. Figure 7-1 shows a simple form we’ve created to view the application state data. Example 7-5 shows the .aspx file that produces the form. Examples 7-6 and 7-7 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 7-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 ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.