Microsoft Knowledge Base Email Alertz

(299683) - 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...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 299683 - Last Review: June 29, 2004 - Revision: 2.2

How To Use the Application Object to Store ASP State

This article was previously published under Q299683

On This Page

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.

Set and Increment the Application Variable in Global.asa

  1. Create a new Web project in Visual InterDev 6.0. For detailed instructions, see the following article in the Microsoft Knowledge Base:
    301184  (http://kbalertz.com/Feedback.aspx?kbNumber=301184/EN-US/ ) How To Create a Visual InterDev Project
  2. In the Project Explorer window, double-click Global.asa to open the Global.asa file.
  3. 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 <SCRIPT> tags in Global.asa.
            Sub Application_OnStart
                    Application("NumVisits") = 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("NumVisits") = Application("NumVisits") + 1 
                    Application("datLastVisited") = Now() 
                    'This line is only for IIS 4.0. Do not use the UnLock
                    'function in IIS 5.0.
                    Application.UnLock
            End Sub
    					
  4. Click Save to save the page, and then close the page.

Create Page1.asp to Display the Hit Counter

  1. 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.
  2. 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 "Last Time of Entry: " & Application("datLastVisited") & "<BR>"
            Response.Write "Number of Visitors: " & Application("NumVisits") & "<BR>"
    %>
    					
  3. Click Save to save the page.
  4. 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.

Complete Code Listing

Global.asa:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
        Sub Application_OnStart
                Application("NumVisits") = 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("NumVisits") = Application("NumVisits") + 1 
                Application("datLastVisited") = 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="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
        Response.Write "Last Time of Entry: " & Application("datLastVisited") & "<BR>"
        Response.Write "Number of Visitors: " & Application("NumVisits") & "<BR>"
%>
</BODY>
</HTML>
				

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.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
175167  (http://kbalertz.com/Feedback.aspx?kbNumber=175167/EN-US/ ) How To Store State in Active Server Pages Applications

APPLIES TO
  • Microsoft Active Server Pages 4.0
  • Microsoft Visual InterDev 6.0 Standard Edition
Keywords: 
kbhowtomaster KB299683
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate