Microsoft KB Archive/299683

= How To Use the Application Object to Store ASP State =

Article ID: 299683

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Visual InterDev 6.0 Standard Edition

-



This article was previously published under Q299683





IN THIS TASK
SUMMARY
 * Set and Increment the Application Variable in Global.asa
 * Create Page1.asp to Display the Hit Counter
 * Complete Code Listing
 * Troubleshooting

REFERENCES



SUMMARY
This document illustrates how to use the Application object to persist data across an Active Server Pages (ASP) application. Items that are stored in the ASP intrinsic Application object are available to all ASP pages in the current application, as well as across all current sessions.

To implement data persistence, you must have Microsoft Internet Information Server 4.0, Internet Information Server 5.0, or Microsoft Personal Web Server 4.0 or later installed and running.

The example in this article demonstrates how to implement a hit counter for your application. The hit counter calculates the number of users that have logged on to the application since the application was last restarted. The example consists of two pages: Global.asa and Page1.asp. In the Global.asa file, the application variable is set and incremented. The Page1.asp file is used to display the hit counter.

back to the top

Set and Increment the Application Variable in Global.asa
 Create a new Web project in Visual InterDev 6.0. For detailed instructions, see the following article in the Microsoft Knowledge Base:

301184 How To Create a Visual InterDev Project

 In the Project Explorer window, double-click Global.asa to open the Global.asa file.  Highlight the following code, right-click the code, and then click Copy. In the Global.asa file, click Paste as HTML on the Edit menu to paste the code between the  tags in Global.asa. Sub Application_OnStart Application(&quot;NumVisits&quot;) = 0 End Sub

Sub Session_OnStart 'This line is only for IIS 4.0. Do not use the Lock 'function in IIS 5.0. Application.Lock Application(&quot;NumVisits&quot;) = Application(&quot;NumVisits&quot;) + 1 Application(&quot;datLastVisited&quot;) = Now 'This line is only for IIS 4.0. Do not use the UnLock 'function in IIS 5.0. Application.UnLock End Sub  Click Save to save the page, and then close the page.

back to the top

Create Page1.asp to Display the Hit Counter
 In the Project Explorer, right-click the project name, click Add, and then click Active Server Page to create a new page named Page1.asp.</li>  Highlight the following code, right-click the code, and then click Copy. In the Page1.asp file, click Paste as HTML on the Edit menu to paste the code between the <BODY> tags in Page1.asp. <%       Response.Write &quot;Last Time of Entry: &quot; & Application(&quot;datLastVisited&quot;) & &quot;<BR>&quot; Response.Write &quot;Number of Visitors: &quot; & Application(&quot;NumVisits&quot;) & &quot;<BR>&quot; %>                   </li> Click Save to save the page.</li> In the Project Explorer window, right-click Page1.asp, and then click View in browser to display the page. Notice that the number of visitors appears as 1. This number is only incremented when a new session starts. Close the browser and browse the page again to increment the number of hits.</li></ol>

back to the top

Complete Code Listing
Global.asa: <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Application_OnStart Application(&quot;NumVisits&quot;) = 0 End Sub

Sub Session_OnStart 'This line is only for IIS 4.0. Do not use the Lock 'function in IIS 5.0. Application.Lock Application(&quot;NumVisits&quot;) = Application(&quot;NumVisits&quot;) + 1 Application(&quot;datLastVisited&quot;) = Now 'This line is only for IIS 4.0. Do not use the UnLock 'function in IIS 5.0. Application.UnLock End Sub </SCRIPT> Page1.asp: <%@ Language=VBScript %> <HTML> <HEAD> <META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;> </HEAD> <BODY> <%       Response.Write &quot;Last Time of Entry: &quot; & Application(&quot;datLastVisited&quot;) & &quot;<BR>&quot; Response.Write &quot;Number of Visitors: &quot; & Application(&quot;NumVisits&quot;) & &quot;<BR>&quot; %> </BODY> </HTML> back to the top

Troubleshooting
You should not use the Application object to store user-specific data; only store application-specific data such as database connection strings.

Also, note that you cannot store Single Threaded Apartment objects in Session or Application scope.

back to the top

<div class="references_section">