Solved

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

Posted on 2008-10-16
14
706 Views
Last Modified: 2013-11-16
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
Comment
Question by:theclassic
  • 7
  • 7
14 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22731160
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22731178
0
 

Author Comment

by:theclassic
ID: 22741346
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22742058
OK, did the link I posted provide enough on the ASPX side?

If so, I'll just work on the Classic ASP side ...
0
 

Author Comment

by:theclassic
ID: 22780868
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22785487
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
 

Author Comment

by:theclassic
ID: 22787149
I will try this...
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:theclassic
ID: 22787589
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22787832

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
 

Author Comment

by:theclassic
ID: 22834143
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
 

Author Comment

by:theclassic
ID: 22834168
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22834278
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
 

Author Comment

by:theclassic
ID: 22835538
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
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22844777
>>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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now