How To Use the Application Object to Store ASP State (299683)



The information in this article applies to:

  • Microsoft Active Server Pages
  • Microsoft Visual InterDev 6.0

This article was previously published under Q299683

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

  1. 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

  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.
back to the top

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.
back to the top

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>
				
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

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

175167 How To Store State in Active Server Pages Applications

back to the top

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbHOWTOmaster KB299683 kbAudDeveloper