theclassic
asked on
Referencing a web.config SQL connection in classic asp...Can it be done?
I want to get rid of all the Connection.asp files on the server, and was wondering if I can use the setting in the web.config file on my server used by the aspx pages for this purpose....
web.config...
<connectionStrings>
<add name="Connect" connectionString="Provider =SQLNCLI; Server=servername ; Database=dbname;Uid=auser; Pwd=pass;" />
to replace this in the asp files...
<!--#include file="Connection.asp"-->
connection.asp
<%
'This is the link to the client's database, change accordingly
Set db=Server.CreateObject("AD ODB.Connec tion")
db.Open "DSN=dbname;UID=auser;PWD= pass"
'replace ??? with the client's subdomain below
subdomain="changethis"
'replace ??? with the client's folder , the register folder should be placed in the client's folder
filepath="changethis"
%>
web.config...
<connectionStrings>
<add name="Connect" connectionString="Provider
to replace this in the asp files...
<!--#include file="Connection.asp"-->
connection.asp
<%
'This is the link to the client's database, change accordingly
Set db=Server.CreateObject("AD
db.Open "DSN=dbname;UID=auser;PWD=
'replace ??? with the client's subdomain below
subdomain="changethis"
'replace ??? with the client's folder , the register folder should be placed in the client's folder
filepath="changethis"
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The web config is at the root - and this will work with classic asp? The connection file is found in many subfolders below the root level, so that is helpful. basically there is a connection.asp file, and then it is referenced by the tag in my post....I will try this and get back to you...Thanks!!!
Yes, that script was designed to work in ASP. It is written in VBScript. JScript wouldn't be a hole lot different...
ASKER
What is the "Encrypt Key" Tag? Is that for the asp or for asp.net also? sorry, begginner....
You can ignore that piece, I was just showing an example of a web.config that one of my sites uses and how differences in the web.config would change the script if you needed to use the script on another site.
ASKER
CCongdon - I need this back in there somewhere - Server.CreateObject("ADODB .Connectio n") - when it was removed, it is causing application errors, particularly
ADODB.Recordset error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
ADODB.Recordset error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
ASKER
Nevermind, i got it -
<%
Dim xmlDoc
Dim connStr
Set xmlDoc = Server.CreateObject("Micro soft.XMLDO M")
xmlDoc.Async = False
xmlDoc.Load(Server.Mappath ("web.conf ig"))
If (xmlDoc.parseError.errorCo de = 0) Then
xmlLoad = True
xmlDoc.setProperty "SelectionLanguage", "XPath"
connStr = xmlDoc.selectSingleNode("/ /add[@name ='ClientCo nnect']"). Attributes .getNamedI tem("conne ctionStrin g").Text
Set db=Server.CreateObject("AD ODB.Connec tion")
db.Open connStr
Else
Response.Write("UNABLE TO FIND CONNECTION")
Response.End()
End If
%>
<%
Dim xmlDoc
Dim connStr
Set xmlDoc = Server.CreateObject("Micro
xmlDoc.Async = False
xmlDoc.Load(Server.Mappath
If (xmlDoc.parseError.errorCo
xmlLoad = True
xmlDoc.setProperty "SelectionLanguage", "XPath"
connStr = xmlDoc.selectSingleNode("/
Set db=Server.CreateObject("AD
db.Open connStr
Else
Response.Write("UNABLE TO FIND CONNECTION")
Response.End()
End If
%>
Yeah, the code sample I gave you just grabbed the connection string from the web.config, but didn't do anything else.
xmlDoc.Load(Server.Mappath
Change "web.config" to a reference relative to the page that is calling the file.
For instance "..\web.config" if the web.config is in a parent directory.