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

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

sloan

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

Mark Moeykens

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;
}
}
 
N

Nick Gilbert

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top