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>

????
theclassicAsked:
Who is Participating?
 
Daniel WilsonCommented:
>>how do you reference them in asp or asp.net?

Put the schema name and a dot before the table name.

Select * from YourSchema.Table1
Select * from MySchema.Table1
0
 
Daniel WilsonCommented:
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?
0
 
Daniel WilsonCommented:
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
theclassicAuthor Commented:
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
0
 
Daniel WilsonCommented:
OK, did the link I posted provide enough on the ASPX side?

If so, I'll just work on the Classic ASP side ...
0
 
theclassicAuthor Commented:
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?
0
 
Daniel WilsonCommented:
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

0
 
theclassicAuthor Commented:
I will try this...
0
 
theclassicAuthor Commented:
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>
 
 
0
 
Daniel WilsonCommented:

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

0
 
theclassicAuthor Commented:
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?
0
 
theclassicAuthor Commented:
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...
0
 
Daniel WilsonCommented:
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.
0
 
theclassicAuthor Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.