Platform SDK: Team Productivity Update

Updating the Global.asa File

Web applications built with Active Server Pages (ASP) typically connect to databases by storing a connection string in the application's Global.asa file. The Global.asa file holds variables that persist for the life of an application.

The Expense Report application uses the OLE DB Provider for SQL Server (SQLOLEDB) to connect to the database. The Global.asa file passes in the value held in ConnectionString each time the application starts:

Sub Application_OnStart
   '--Miscellaneous application settings
Application("ConnectionString")  = "Provider=sqloledb;" & _
                "Data Source=(local);" & _
                "Initial Catalog=expense;" & _
                "Trusted_Connection=yes;"

Each instance of the Expense Report application the Team Productivity Update creates must have its Global.asa file updated.

The following code sample opens the Global.asa file to read the ConnectionString variable used to connect to the database. It uses the FileSystemObject object to open the file, passing it the WebPath variable that holds the virtual directory name for the application, and the value 1 to set the optional iomode parameter to ForReadOnly. The script then reads the text in the file and closes it. This sets the value of the GlobalAsaText variable to the contents of the Global.asa file. This value is used by the next section of code, a call to the VBScript Replace function, to change the contents of the Global.asa file.

' Update global.asa's connection string to point to correct database
Set oFile = oFileSystem.OpenTextFile(WebPath & "\global.asa", 1) ' Read
GlobalAsaText = oFile.ReadAll  
oFile.Close

Next, the script changes the contents of the Global.asa by calling the VBScript Replace function. The function's parameters pass in the Global.asa text to be modified, which is the value held by the GlobalAsaText variable, the substring it needs to find, and the new value for the substring, "Initial Catalog=" & DatabaseName & ";". The value of DatabaseName was set in Creating the Expense Report Application Physical Directory to the values held by the SourceName variable and the NextInstanceNumber variable.

GlobalAsaText = Replace(GlobalAsaText,"Initial Catalog=" & SourceName & ";", "Initial Catalog=" & DatabaseName & ";")

Last, the script uses the FileSystem object to write the new string pointing to the correct database to the Global.asa file, closes the file, and deactivates the FileSystem object.

Set oFile = oFileSystem.OpenTextFile(WebPath & "\global.asa", 2) ' Write
oFile.Write GlobalAsaText
oFile.Close
Set oFileSystem = Nothing

To follow the next steps in the script's execution, see Creating and Pointing to the Virtual Directory.