Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 790
  • Last Modified:

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>

????
0
theclassic
Asked:
theclassic
  • 7
  • 7
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now