Link to home
Start Free TrialLog in
Avatar of theclassic
theclassicFlag for United States of America

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("ADODB.Connection")
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"
%>
ASKER CERTIFIED SOLUTION
Avatar of CCongdon
CCongdon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh yeah, and in case the web.config is somewhere else besides in the same directory as the ASP page looking for the connection string change this line:
xmlDoc.Load(Server.Mappath("web.config"))

 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.
Avatar of theclassic

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...
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.
CCongdon - I need this back in there somewhere - Server.CreateObject("ADODB.Connection") - 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.
Nevermind, i got it -

<%
Dim xmlDoc
Dim connStr
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(Server.Mappath("web.config"))
If (xmlDoc.parseError.errorCode = 0) Then
  xmlLoad = True
  xmlDoc.setProperty "SelectionLanguage", "XPath"
  connStr = xmlDoc.selectSingleNode("//add[@name='ClientConnect']").Attributes.getNamedItem("connectionString").Text
  Set db=Server.CreateObject("ADODB.Connection")
  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.