Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Example Global.asa for connection string.

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
0
0
US
Stupid questions I know, bear with me. I was instructed way back when to always store your connection strings within the global.asa file for your websites. Can someone tell me if this is the proper way to write a global.asa file?

Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=NameofDSN;UID=UserID;PWD=Password"

 
Also do you HAVE to store your connection string in a global.asa in ASP classic? And do you reference the global.asa file via an include statement or no?

 
No you don't have to do any of that.

The global.asa is just an easy place to initialize any Application or Session variables because, if the this file exists, it automatically called by ASP so you don't have to remember to include it.
 
OK I kinda forgot to answer the connection string question... sorry!

It can be handy to store your connection string in an Application variable set in the global.asa so that, when it eventually changes, you can update it in the one spot instead of in every file on your site... kinda like an include file that you don't need to include.

It could be somethingn like this:
Code:
<script language="vbscript" runat="server">
  sub Application_OnStart
    Application("MyCN") = "DSN=NameofDSN;" _
                        & "UID=UserID;" _
                        & "PWD=Password"
  end sub
</script>


Then you could use it like this:[tt]
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("MyCN")
[/tt]

See here for more:
 
is that asp or asp.net?

And is it required to store your connections string there, or is it acceptable in another file?

 
The code I posted was ASP classic and no, you don't have to put anything in the global.asa... you could leave it empty or probably even just delete it if you aren't using it.

Its just a handy place to do some web app setup work.
 
Also remember if you are getting an error saying the SQL Server was not found...

... then by definition ASP must have known to even look for a SQL Server in the first place...

... so ASP must have had access to the connection string ...

... because the ADO connection isn't gonna just assume to go looking for that DB without being told to do so ...


If you want to be sure, you could add some error trapping code that gives more info:
Code:
MyCNString = "blah blah"  [green]'ADO connection string set up here[/green]

Set Conn = Server.CreateObject("ADODB.Connection")
[red] On Error Resume Next[/red]
Conn.Open MyCNString
[red]If Err.Number <> 0 then
  Response.Write "Error #" & Err.Number & "<BR>" & vbCrLf
  Response.Write "Source: " & Err.Source & "<BR>" & vbCrLf
  Response.Write "Description: " & Err.Description & "<BR>" & vbCrLf
  Response.Write "ADO Connection: " & MyCNString & "<BR>" & vbCrLf
End If[/red]

... although if it is a live site then you might want to send the connection string in an email to yourself instead of displaying it to the user... especially if it includes a private username/password.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top