How do you get a connection string from web.config in ASP.NET 2.0?

  • Thread starter Thread starter Nick Gilbert
  • Start date Start date
N

Nick Gilbert

Hi,

How on earth do you get a connection string stored like this in ASP.NET 2?

<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="blah" />
</connectionStrings>
<configuration>

If I try this (which I've seen in LOADS of online examples:

WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString

....I get the following error:

Cannot apply indexing with [] to an expression of type
'System.Configuration.ConnectionStringSettingsCollection'

What am I doing wrong?!

Nick...
 
The most obvious thing I see is:

<add name="Pubs" connectionString="blah" />


The "name" is the uniqueidentifer.

A "name" of "ConnectionString" is very confusing and ambigious.
 
Even though he's doing that, I don't get the same error he does. Instead I
get the "Object reference not set to an instance of an object." because he's
looking for "Pubs" but the name of the connection string is
"ConnectionString".

Nick,
I can't reproduce the same error you're mentioning. Here is how I tried to
reproduce your problem:
---------------------------------------------------------------
web.config
---------------------------------------------------------------
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear/>
<add name="Pubs" connectionString="blah" />
</connectionStrings>
</configuration>
---------------------------------------------------------------
Notice I changed the name attribute to "Pubs".

Here's my code in my aspx code behind:
---------------------------------------------------------------
default.aspx.cs
---------------------------------------------------------------
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblConnString.Text =
WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
}
}
 
It turns out I was missing a reference to System.configuration from the
business layer project the code was in. Aaagh!

All working fine now - thanks.

Nick...

Mark said:
Even though he's doing that, I don't get the same error he does. Instead I
get the "Object reference not set to an instance of an object." because he's
looking for "Pubs" but the name of the connection string is
"ConnectionString".

Nick,
I can't reproduce the same error you're mentioning. Here is how I tried to
reproduce your problem:
---------------------------------------------------------------
web.config
---------------------------------------------------------------
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear/>
<add name="Pubs" connectionString="blah" />
</connectionStrings>
</configuration>
---------------------------------------------------------------
Notice I changed the name attribute to "Pubs".

Here's my code in my aspx code behind:
---------------------------------------------------------------
default.aspx.cs
---------------------------------------------------------------
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblConnString.Text =
WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
}
}
---------------------------------------------------------------

This seems to work for me.
Mark Moeykens

Mark Rae said:
Adding a connection string called "ConnectionString" in web.config, and then
trying to retrieve a connection string called "Pubs"...
 
Back
Top