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!

ConnectionStrings setting in web.config VS 2010 not working like 2008

Status
Not open for further replies.

Gzk2010

Programmer
Aug 17, 2010
48
US
Hi I moved my vs 2008 'still in development' web app to vs 2010.
I get a Null reference error on the below code behind. I hard coded the connection string straight in the C# code behind to test it and works fine. I do have the 'using System.Web.Configuration;' up top.

Two things confuse me:
1. there is no app settings node in the config file, should I just put it in.
2. I try to convert to web application and does not take?

code behind:
private String connectionString =
WebConfigurationManager.ConnectionStrings["GZK"].ConnectionString;


web.config debug :


<connectionStrings>
<add
name="GZK"
connectionString="Data Source=grantorino\enterprise;Initial
Catalog=GZK;Persist Security Info=True;User
ID=GZK;Password=xxxxx"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
 
I just use ConfigurationManager.ConnectionStrings in my web application.
There should be a Connectionstrings node straight under the configuration node in web.config.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks, but still no dice. the line below says connectionString has no context and is underlined in red.
SqlConnection myCon = new SqlConnection(ConnectionString);

Any Ideas?
 
you need
Code:
using System.Configuration;
not
Code:
using System.Web.Configuration;
 
I figured it out and boy do I feel like a dodo head. I copied the:

<add
name="GZK"
connectionString="Data Source=grantorino\enterprise;Initial Catalog=GZK;Persist Security Info=True;User ID=GZK;Password=xxxxxx"
providerName="System.Data.SqlClient" />
from the web.config from vs 2008 to the web.config.debug in vs2010. in vs 2008 of course you do not have that option but in vs 2010, I made a best guess to put in debug over release.config since it was running in debug mode. Today I clicked on just web.config itself and realised it needed to be there. dahhh

Thanks for your help
 
I know its a little off topic, apologies for that, but in reference to whosrdaddy it's recomended practive to use WebConfigurationManager rather than ConfigurationManager in web based applications as it gives you access to web.Config specific elements.

To quote MSDN;
Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. For client applications, use the ConfigurationManager class

.NET Framework 4 WebConfigurationManager Class

.NET Framework 3.5 WebConfigurationManager Class

.NET Framework 3.0 WebConfigurationManager Class

.NET Framework 2.0 WebConfigurationManager Class


Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
thanks for clearing that up Rhys!

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top