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

Discussion in 'ASP .Net' started by Nick Gilbert, Oct 23, 2008.

  1. Nick Gilbert

    Nick Gilbert Guest

    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...
    Nick Gilbert, Oct 23, 2008
    #1
    1. Advertising

  2. Nick Gilbert

    sloan Guest

    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" <> wrote in message
    news:%...
    > 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...
    sloan, Oct 23, 2008
    #2
    1. Advertising

  3. Re: How do you get a connection string from web.config in ASP.NET

    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" <> wrote in message
    > news:%...
    >
    > > 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
    >
    >
    Mark Moeykens, Oct 23, 2008
    #3
  4. Nick Gilbert

    Nick Gilbert Guest

    Re: How do you get a connection string from web.config in ASP.NET

    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" <> wrote in message
    >> news:%...
    >>
    >>> 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
    >>
    >>
    Nick Gilbert, Oct 24, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gregory_may
    Replies:
    1
    Views:
    594
    Bruce Barker
    Nov 21, 2005
  2. Q. John Chen
    Replies:
    0
    Views:
    370
    Q. John Chen
    Jan 16, 2006
  3. Mark
    Replies:
    1
    Views:
    4,677
    Christopher Reed
    Feb 17, 2006
  4. CSharpner
    Replies:
    0
    Views:
    996
    CSharpner
    Apr 9, 2007
  5. Replies:
    3
    Views:
    836
    Samuel R. Neff
    Aug 3, 2007
Loading...

Share This Page