The 'global.asa' file contains your 4 basic server events.
1) Application_OnStart - This happens when server is restarted or when global.asa file is saved.
2) Application_OnEnd - This happens right before server gets shut down
3) Session_OnStart - This happens when someone views your webpage
4) Session_OnEnd - This happens when someone quits viewing your webpage. Actually, this happens after the session expires, which can be set by: Session.Timeout=20 (This would set session length to 20 minutes).
You'll notice I set 2 application variables called 'SQL_DSN' and 'MySQL_DSN' equal to generic connection strings. Now, As I need to make database calls, I simply refer back to:
Now, after I code the 4 server events, I like to include the 'METADATA ADO Type Library' tag so that I don't have to include the 'adovbs.inc' file on any page that requires a connection to the database.
Also, you can't set cookies on the global.asa page! So don't try it, cause it won't work.
Here's the Global.asa file below or CLICK HERE for .txt version.
GLOBAL.ASA, (must be located in root directory)
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnStart
Application("SQL_DSN")="DRIVER=SQL Server;UID=<USERNAME>;PWD=<PASSWORD>;Address=<Computer running SQL Server>,1433;Network=DBMSSOCN;DATABASE=<DATABASE NAME>;SERVER=<URL of SQL Server>"
Application("MySQL_DSN")="driver={MySQL};server=<Computer running MySQL>;database=<DATABASE NAME>;uid=<USERNAME>;pwd=<PASSWORD>;"
END SUB
SUB Application_OnEnd
END SUB
SUB Session_OnStart
END SUB
SUB Session_OnEnd
END SUB
</SCRIPT>
<!-- METADATA TYPE="typelib" UUID="00000200-0000-0010-8000-00AA006D2EA4" NAME="ADO Type Library" -->
Make your own custom global.asa file:
Here are a few generic functions I like to use when I'm feelin' like savin' time. Notice that I'm connecting to an SQL database using my Application("SQL_DSN") variable.
Also notice that you'll need to edit the 'CommandText' and the Function names.
<script LANGUAGE="VBScript" RUNAT="Server">
Function FUNCTION_NAME()
Dim Cmd
Set Cmd = Server.CreateObject("ADODB.Command")
Cmd.ActiveConnection = Application("SQL_DSN")
Cmd.CommandText = "SQL_GOES_HERE"
Cmd.CommandType = adCmdText
Set FUNCTION_NAME = Cmd.Execute
End Function
Function FUNCTION_NAME_STORED_PROC(ITEM1,ITEM2,ITEM3, ITEM4)
Dim Param, Cmd
Set Cmd = Server.CreateObject("ADODB.Command")
Cmd.ActiveConnection = Application("SQL_DSN")
Cmd.CommandText = "STORED_PROC_NAME_GOES_HERE"
Cmd.CommandType = adCmdStoredProc
Set Param = Cmd.CreateParameter("@strVarName1", adBoolean, adParamInput, 1, ITEM1) 'Bit
Cmd.Parameters.Append Param
Set Param = Cmd.CreateParameter("@strVarName2", adInteger, adParamInput, 4, ITEM2) 'Integer
Cmd.Parameters.Append Param
Set Param = Cmd.CreateParameter("@strVarName3", adVarChar, adParamInput, 255, ITEM3) 'STRING - VarChar
Cmd.Parameters.Append Param
Set Param = Cmd.CreateParameter("@strVarName4", adLongVarChar, adParamInput, len(ITEM4), ITEM4) 'TEXT/IMAGE Data Type
Cmd.Parameters.Append Param
Set FUNCTION_NAME_STORED_PROC = Cmd.Execute
End Function
Function FUNCTION_NAME_CURSORS(page,pagesize)
Dim cnn, rs
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.Open Application("SQL_DSN")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = adOpenStatic
rs.PageSize = pagesize
rs.Open "SQL_GOES_HERE",Application("SQL_DSN"),1,2
rs.AbsolutePage = page
Set FUNCTION_NAME_CURSORS = rs
End Function
'-------Here's some great functions for a simple connection to the database,
to execute an SQL command, or to convert an HTML page to an .asp string.
Function Connect()'-----'Connect' acts as object, - Connect.Execute(SQL)
Dim cnn
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.Open DSN
Set Connect = cnn
End Function
Function CommandSQL(sSQL) '------------- EX.: Set rs = CommandSQL("SELECT * from table")
Dim Cmd
Set Cmd = Server.CreateObject("ADODB.Command")
Cmd.ActiveConnection = DSN
Cmd.CommandType = adCmdText '2
Cmd.CommandText = sSQL
Set CommandSQL = Cmd.Execute
End Function
Function HTMLToASP(s) ' This converts an HTML page to an .asp string
Dim ss
ss = s
ss = Replace(ss,"<" & "%","<%")
ss = Replace(ss,"%" & ">","%>")
ss = Replace(ss,"""","""""")
ss = Replace(ss,"<","<")
ss = Replace(ss,">",">")
ss = Replace(ss,vbcrlf,""" & "&_" & vbcrlf & vbtab & """)
ss = vbtab & """ & ss & """ & vbcrlf
HTMLToASP = ss
End Function
</script>