theclassic
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.Sql Client" %>
<%@ Import Namespace="microsoft.visua lbasic" %>
<script language="vb" runat="server">
''Connects to original database
Public indexConn As String = "data source='SMARTLINK-01.SERVE R.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("AD ODB.Connec tion")
db.Open "DSN=asp;UID=userasp;PWD=p assasp"
'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.SERVE R.COM' ; Database=NAME;Uid=user;Pwd =pass;"/>
<add name="aspconnect" connectionString="Server=' SMARTLINK- 01.SERVER. COM'; Database=asp;Uid=userasp;P wd=passasp ;"/>
</connectionStrings>
????
Here is the current connection references used
In the aspx file...
<%@ Page debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<%@ Import Namespace="microsoft.visua
<script language="vb" runat="server">
''Connects to original database
Public indexConn As String = "data source='SMARTLINK-01.SERVE
connection.asp -
<%
'This is the link to the client's database, change accordingly
Set db=Server.CreateObject("AD
db.Open "DSN=asp;UID=userasp;PWD=p
'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
<add name="aspconnect" connectionString="Server='
</connectionStrings>
????
If you're wondering how to get the ASP.net to use those settings, see https://www.experts-exchange.com/questions/23800539/Developing-locally-and-deploying-remotely-database-access.html and http://msdn.microsoft.com/en-us/library/ms178411.aspx
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.SERVE R.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
<!--#include file="Connection.asp"-->
And this code in the aspx pages
Public indexConn As String = "data source='SMARTLINK-01.SERVE
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 ...
If so, I'll just work on the Classic ASP side ...
ASKER
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 ...
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
ASKER
I will try this...
ASKER
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=user id;Pwd=pas sword;"/>
</connectionStrings>
<connectionStrings>
<add name="InternalConnect" connectionString="Provider
</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
ASKER
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?
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?
ASKER
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.
My code is in the ASP page that tries to read it & use the connection string.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?