ConfigurationSettings in Class Library

Discussion in 'ASP .Net' started by Random, Dec 29, 2004.

  1. Random

    Random Guest

    I'm writing a class library for my web app data access, but don't want to
    hard code the database connection string in the class. Nor do I want to
    pass the connection string in from the web app every time I access it. I
    want to define the connection string in the web app ConfigurationSettings
    and let the class library reference it itself whenever it needs a
    connection.

    How can I do this?
     
    Random, Dec 29, 2004
    #1
    1. Advertising

  2. Hello ?,

    I don't understand why you don't want to put the connection string in the
    web.config file. You can use can add a key to the appSettings node and
    reference it in code:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="myconnstring" value="connectionstringgoeshere" />

    In code:

    db.connstring = ConfigurationSettings.AppSettings("myconnstring")

    Ken

    "Random" <> wrote in message
    news:...
    > I'm writing a class library for my web app data access, but don't want to
    > hard code the database connection string in the class. Nor do I want to
    > pass the connection string in from the web app every time I access it. I
    > want to define the connection string in the web app ConfigurationSettings
    > and let the class library reference it itself whenever it needs a
    > connection.
    >
    > How can I do this?
    >
     
    Ken Cox [Microsoft MVP], Dec 29, 2004
    #2
    1. Advertising

  3. Random

    Random Guest

    That's precisely where I do want to put it. My class library, however, is
    not a part of the web application, but would be a referenced dll that runs
    within the HttpContext.


    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > Hello ?,
    >
    > I don't understand why you don't want to put the connection string in the
    > web.config file. You can use can add a key to the appSettings node and
    > reference it in code:
    >
    > <?xml version="1.0" encoding="utf-8" ?>
    > <configuration>
    > <appSettings>
    > <add key="myconnstring" value="connectionstringgoeshere" />
    >
    > In code:
    >
    > db.connstring = ConfigurationSettings.AppSettings("myconnstring")
    >
    > Ken
    >
    > "Random" <> wrote in message
    > news:...
    >> I'm writing a class library for my web app data access, but don't want to
    >> hard code the database connection string in the class. Nor do I want to
    >> pass the connection string in from the web app every time I access it. I
    >> want to define the connection string in the web app ConfigurationSettings
    >> and let the class library reference it itself whenever it needs a
    >> connection.
    >>
    >> How can I do this?
    >>

    >
     
    Random, Dec 29, 2004
    #3
  4. Random

    Matt Berther Guest

    Hello Random,

    So?

    ConfigurationSettings references the config file of the application using
    it. This should not be a problem for you. A referenced assembly can use the
    ConfigurationSettings of the parent application.

    --
    Matt Berther
    http://www.mattberther.com

    > That's precisely where I do want to put it. My class library,
    > however, is not a part of the web application, but would be a
    > referenced dll that runs within the HttpContext.
    >
    > "Ken Cox [Microsoft MVP]" <> wrote in
    > message news:...
    >
    >> Hello ?,
    >>
    >> I don't understand why you don't want to put the connection string in
    >> the web.config file. You can use can add a key to the appSettings
    >> node and reference it in code:
    >>
    >> <?xml version="1.0" encoding="utf-8" ?>
    >> <configuration>
    >> <appSettings>
    >> <add key="myconnstring" value="connectionstringgoeshere" />
    >> In code:
    >>
    >> db.connstring = ConfigurationSettings.AppSettings("myconnstring")
    >>
    >> Ken
    >>
    >> "Random" <> wrote in message
    >> news:...
    >>
    >>> I'm writing a class library for my web app data access, but don't
    >>> want to hard code the database connection string in the class. Nor
    >>> do I want to pass the connection string in from the web app every
    >>> time I access it. I want to define the connection string in the web
    >>> app ConfigurationSettings and let the class library reference it
    >>> itself whenever it needs a connection.
    >>>
    >>> How can I do this?
    >>>
     
    Matt Berther, Dec 31, 2004
    #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. Bassel Tabbara [MSFT]

    RE: ConfigurationSettings.AppSettings Error

    Bassel Tabbara [MSFT], Jul 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    702
    Bassel Tabbara [MSFT]
    Jul 7, 2003
  2. Rob Mayo
    Replies:
    6
    Views:
    456
    TJoker .NET [MVP]
    Sep 22, 2003
  3. Guest
    Replies:
    3
    Views:
    3,723
    =?Utf-8?B?TG9yaSBNY0RvbmFsZA==?=
    Feb 23, 2004
  4. Andrew Fisher
    Replies:
    3
    Views:
    449
    Andrew Fisher
    Jan 28, 2005
  5. Brad Wood
    Replies:
    1
    Views:
    127
Loading...

Share This Page