Storing and Using Application Settings

The AppSettings.asp page presents a form called frmSettings, which lets administrators edit these settings. Clicking the Update Settings button calls a client-side subroutine called ChangeSettings. ChangeSettings first verifies that all the required fields have had values input:

' Check for empty strings in required values
aFields = Array("ExchangeServer","ExchangeSite","ExchangeOrg","CheckoutPeriod","RecallPeriod","DefaultLocation")
For Each item In aFields
   If Trim(theForm.Elements(item).Value) = "" Then
      strMissing = strMissing & item & vbCrLf
   End If
Next

The code in this file then verifies that numeric values have been input for Days before overdue and Days between notices. If all values are present and valid, the form is submitted. The server-side code to process this form is on the same page, AppSettings.asp. After first verifying that the form has just been posted, this code opens the settings table in the FmLib database and saves these values into it. It also saves these values as properties on the ASP Application object.

These values have now been stored once in a temporary location (in the Application object) and once in a permanent location (the FmLib database). Because the temporary storage location is in memory, it can be accessed quickly, and it is these values that are used by the application at run time. For example, Checkin.asp displays the default location for library materials being checked in:

<TD>Location:</TD>
        <TD><INPUT type="textbox" name="txtLocation" value="<%= Application("DefaultLocation")

The use of permanent storage is to initialize the values in the Application object when the application starts. The following code, from global.asa, initializes all the application settings at once by reading them from the settings table in the FmLib database:

Sub ReadApplicationSettings()
   Dim rs,fld
   Set rs = Server.CreateObject ("ADODB.Recordset")
   rs.Open "Settings", Application("FmLib_ConnectionString"), adOpenForwardOnly, adLockReadOnly, adCmdTable
   If Not rs.EOF Then
      For Each fld In rs.Fields
         If LCase(fld.Name) = "debug" Then
            '--- Special case for boolean value
            Application("Debug") = (fld.Value = 1)
         Else
            Application(fld.Name) = Trim(CStr(fld.Value))
         End If
      Next
   End If
   rs.Close
   Set rs = Nothing
End Sub

Storing application settings in a table in the FmLib database has the following advantage: In cases where using these settings requires access to other data in the FmLib database, the CML's stored procedures can participate in the action. If they were stored outside the database, such as in a text file or in the system registry, it would be difficult to use their values in conjunction with stored procedures.