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

asked on

Setting Connection Strings in the web.config file as oppossed to embedded

I have an classic asp site, with some asp.net webforms acting as a content manager - I want to change the ap[plications to use a universal web.config file for the connections, and want to use the best method....

Here is the current connection references used
In the aspx file...

<%@ Page debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="microsoft.visualbasic" %>


<script language="vb" runat="server">

''Connects to original database
Public indexConn As String = "data source='SMARTLINK-01.SERVER.COM'; Database='NAME'; User ID='user'; Password='pass'; Persist Security Info=True;packet size=4096"


connection.asp -
<%

'This is the link to the client's database, change accordingly
Set db=Server.CreateObject("ADODB.Connection")
db.Open "DSN=asp;UID=userasp;PWD=passasp"

'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"
%>
 

and then in the other asp pages...

<!--#include file="Connection.asp"-->

So I want to know how to declare the new web.config keys i have written, and replace the old declarations

new web.config

<connectionStrings>
    <add name="aspnetcontentconnect" connectionString="Provider=SQLNCLI; Server='SMARTLINK-01.SERVER.COM' ; Database=NAME;Uid=user;Pwd=pass;"/>
    <add name="aspconnect" connectionString="Server='SMARTLINK-01.SERVER.COM'; Database=asp;Uid=userasp;Pwd=passasp;"/>
  </connectionStrings>

????
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Sorry, I'm not sure I understand the question.

Is it the ASP.Net you're trying to get to use the web.config settings?  Or the Classic ASP?  Or am I still missing the question?
Avatar of theclassic

ASKER

Sorry - I want to replace this tag in my classic asp pages, which is

<!--#include file="Connection.asp"-->
 
And this code in the aspx pages
Public indexConn As String = "data source='SMARTLINK-01.SERVER.COM'; Database='NAME'; User ID='user'; Password='pass'; Persist Security Info=True;packet size=4096"
 
To reference the connection I have added to a new web config file.....Sorry
OK, did the link I posted provide enough on the ASPX side?

If so, I'll just work on the Classic ASP side ...
Hi - I am having trouble with your example - can you just show an example of how to do an ole db sql connection in asp?
Good example of both is at http://www.c-sharpcorner.com/UploadFile/sd_patel/WebConfigInASPNet11242005061608AM/WebConfigInASPNet.aspx

Modifying the Classic ASP example a little ...


set xmlDoc=server.CreateObject("Microsoft.XMLDOM")
set xmlappSettings=server.CreateObject("Microsoft.XMLDOM")
set xmladd=server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(server.MapPath ("web.config"))
set xmlappSettings = xmldoc.GetElementsByTagName("appSettings").Item(0)
set xmladd = xmlappSettings.GetElementsByTagName("add")
for each x in xmladd
'Check for the Atrribute Value
if  x.getAttribute("key") ="ConnectionString1" then
  indexConn  = x.getAttribute("value"))
end if
next

Open in new window

I will try this...
Is there a response write that can tell me whether or not this is working....I am basically naming a file connection.asp and the webconfig - would look it like this?
 
<connectionStrings>
    <add name="InternalConnect" connectionString="Provider=SQLNCLI; Server=servername ; Database=internal;Uid=userid;Pwd=password;"/>  
  </connectionStrings>
 
 

set xmlDoc=server.CreateObject("Microsoft.XMLDOM")
set xmlappSettings=server.CreateObject("Microsoft.XMLDOM")
set xmladd=server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(server.MapPath ("web.config"))
set xmlappSettings = xmldoc.GetElementsByTagName("appSettings").Item(0)
set xmladd = xmlappSettings.GetElementsByTagName("add")
for each x in xmladd
'Check for the Atrribute Value
if  x.getAttribute("key") ="InternalConnect" then
  indexConn  = x.getAttribute("value"))
  Response.Write(indexConn)
end if
next

Open in new window

It is not working..also, I think you meant "name" instead of "key" right?
Do I need to place that code in a webform or something?  I basically pasted it exactly as you have it, saved it at the root with the web.config, and it says "Page could not be displayed in the browser" - is there another way to test it?
I have a web config, with a named string called "Internal Connect", can we pass it to asp and do a response write based on whether it is even working?  Just wanted to say what I was asking again...
The block in your comment from 10/23 is what you put in the web.config.

My code is in the ASP page that tries to read it & use the connection string.
I tried it, no luck - posted exactly as you had it.  I'll give you the points if you want, because it looks close.  Maybe you could tell me a little about how to differentiate between two tables with the same exact name in a database that exist because they are part of different schemas - how do you reference them in asp or asp.net?  For the answer to my question, below is the code that worked for me...

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='InternalConnect']").Attributes.getNamedItem("connectionString").Text
Else
  Response.Write("UNABLE TO FIND CONNECTION")
  Response.End()
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
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