Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConfigurationManager problem 1

Status
Not open for further replies.

PatrickIRL

Programmer
Jun 25, 2003
383
Hi, Starting my first web app using ASP.NET and I'm storing the connection string in the web.config:

Code:
<connectionstrings>
<add name="foo" connectionstring="provider=SQLOLEDB.1;Data source=<source>;Initial Catalog=<database>;UID=<user>;PWD=<pwd>";
</connectionstrings>

Problem is when I assign the value stored in "foo" to a string:
Code:
string strCN=ConfigurationManager.ConnectionStrings["foo"].ConnectionString;

and then attempt to open a connection using that I get an error: Can't find server or no permissions: Invalid string

BUT if I use the same connectionstring without using the ConfigurationManager it works fine!

This works and connects to the database and displays the correct data:

Code:
cn.connectionstring="provider=SQLOLEDB.1;Data source=<source>;Initial Catalog=<database>;UID=<user>;PWD=<pwd>";

Any ideas???

Thanks in advance, Patrick
 
if you have non-alphanumeric characters in your database, user or pwd values this could be the problem. I ran into this with ampersand (&). the web.config required &amp;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, thanks for the reply. Nothing out of the ordinary in the database name, server name, uid or pwd. It's really frustrating when I know the connection string works.

Patrick

(just an aside, I've been browsing this and the C# forums a bit and I'm always impressed by your knowledge and answers. In fact I've ordered the "Head First" book)
 
thank you for the compliment. if your interested other materials I have found to be very useful are:
books:
Domain Driven Design
C# via CLR

blogs:
jpboodhoo.com (look in early 2008 and 2007 archives for technical goodness, lately his focus is empowering people)
codebetter.com
ayende.com
lostechies.com

as for your immediate problem. how are you opening the connection?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I open the connection the way I've done it previously; set up the connectionstring then assign the value to the connectionstring property of the connection and open the connection. I don't use the "using" statement the way I've seen here, just want to get it working first!

Patrick
 
i would create a unit test to ensure the value is read correctly
Code:
var expected = "provider=SQLOLEDB.1;Data source=<source>;Initial Catalog=<database>;UID=<user>;PWD=<pwd>";
var actual = ConfigurationManager.ConnectionStrings["foo"].ConnectionString;
Assert.AreEqual(expected, actual);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Ah yes, couldn't see the wood for the trees and all that. My connection string in the web.config was ALMOST the same as my regular connection string, I must have copied and pasted the wrong one here.

The problem was in the "Data Source" section of the string, in my regular connection it was "Data Source" but in the web.config it was "DataSource". Funny how the more you look the less you see.

Thanks for the help Jason, much appreciated.

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top