Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How do you get a connection string from web.config in ASP.NET 2.0?

Reply
Thread Tools

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

 
 
Nick Gilbert
Guest
Posts: n/a
 
      10-23-2008
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.ConnectionStringSettingsColl ection'

What am I doing wrong?!

Nick...
 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      10-23-2008
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.




"Nick Gilbert" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.ConnectionStringSettingsColl ection'
>
> What am I doing wrong?!
>
> Nick...



 
Reply With Quote
 
 
 
 
Mark Moeykens
Guest
Posts: n/a
 
      10-23-2008
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 [MVP]" wrote:

> "Nick Gilbert" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
> > What am I doing wrong?!

>
> Adding a connection string called "ConnectionString" in web.config, and then
> trying to retrieve a connection string called "Pubs"...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
>
>

 
Reply With Quote
 
Nick Gilbert
Guest
Posts: n/a
 
      10-24-2008
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 Moeykens wrote:
> 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 [MVP]" wrote:
>
>> "Nick Gilbert" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>
>>> What am I doing wrong?!

>> Adding a connection string called "ConnectionString" in web.config, and then
>> trying to retrieve a connection string called "Pubs"...
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net
>>
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to set the connection pool size in the Sybase connection string? Wei Lu ASP .Net 2 02-16-2009 09:51 AM
Web.config: connection string or connection key? rlueneberg@gmail.com ASP .Net 3 08-03-2007 09:37 PM
how do you duck-type something to String, so String believes you? Sam Roberts Ruby 10 03-20-2005 03:50 AM



Advertisments