To read the article online, visit

Saving Application Variables Across Web Restarts

By Daniel Cooper

A while ago I had a problem with a site that I was working on. One of the requirements called for a featured product to appear in the footer of the site, which would be changed monthly by the client through a back-end interface.

Usually when one has to dynamic information for a site a database is the answer. In this case I thought "This is stupid. I'm not going to do a database call to retrieve a single value and I'm not going to have an orphan table in the database to only hold that value!"

The obvious solution is to use an application variable. After all, itís global, can be set easily and have little overhead. The problem is that the webserver gets rebooted every week, meaning that any settings in the application variables would be lost. My problem was: "How do I make application wide values persistent?"

What I did was write two functions. One writes key/value pairs to a text file in a format that the second function can read back. I set up the site so that when the site's operator set a value, it was written both to the text file and to an application variable. Then when the application restarts, the Global.asa file contains code to initialize those variables to the previous values set. Note that this approach will only persist textual application variables since there is no way to represent a binary object through a text file.

So the code to record application variable values looks a little like this:

Application("featured_product_id")  =  someVariable
Call recordToFile(someFileName, "featured_product", someVariable)

And in the Application_OnStart event in Global.asa we have a line like:

Application("featured_product_id") = readFromFile(fileName, "featured_product")

So once you set a value in both an application variable and in the file you can access it throughout the site with little overhead and the confidence of knowing that if your web application is restarted the value will persist.

Here's how its done:

  • recordToFile() - writes a key/value pair to a text file you specify, I use data.txt but it can be any filename. It creates the file if it doesnít already exist and will create a key/value pair or set the value if it exists.
  • readFromFile() - opens your data file and retrieves the value for a given key, or null if it canít find it.

Following is the code to recordToFile. In Part 2 we'll look at the code for readFromFile and dissect both functions.

  • Part 2

    function recordToFile(strFileName, strKey, strValue)
      dim objFileSys, objFile, arrLine, objDict, thing
      Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
      Set objDict = Server.CreateObject("Scripting.Dictionary")
      if objFileSys.FileExists(strFileName) then
        'case where file already exists and presumably holds values
        Set objFile = objFileSys.OpenTextFile(strFileName, 1)
        do while not objFile.AtEndOfStream
          arrLine = split(objFile.ReadLine, "|")
          if isArray(arrLine) then
            if Ubound(arrLine) = 1 then
              objDict.Add arrLine(0), arrLine(1)
            end if
          end if
      end if
      'insert/update new item
      objDict.Item(strKey) = strValue
      Set objFile = objFileSys.OpenTextFile(strFileName, 2, true)
      for each thing in objDict
        objFile.writeLine thing & "|" & objDict.Item(thing)
      Set objDict = Nothing
      Set objFile = Nothing
      Set objFileSys = Nothing
    End Function

  • Article Information
    Article Title: Saving Application Variables Across Web Restarts
    Article Author: Daniel Cooper
    Published Date: Friday, June 02, 2000
    Article URL:

    Copyright 2019 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers