Results 1 to 3 of 3

Thread: [RESOLVED] How to access a connection string from a web.config or app.config in a class library

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Resolved [RESOLVED] How to access a connection string from a web.config or app.config in a class library

    Hi Guys,

    I have a class library that I've used as a DataAccessLayer in my website. In the web.config file for the website I defined a connectionstring.
    In the class library I accessed the connectionstring like this:

    Code:
     Public Class MySchoolDataAccessLayer
            Private Shared strCon As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("constr").ConnectionString
    However, today I tried creating a Windows project and referenced the dll. When I instantiate an instance of the class in the dll that references the connectionstring above I get a type initializer error "object instance not set to an instance of an object"

    I know it's because it's a windows project and the dll is using the webconfiguartionmanager which is meant for asp.net.

    My question is, how can I get the connectionstring in my class library based on the type of project it is referenced in. If I refrence the dll in a web project I need to use the WebConfigurationManager, and if I use it in a windows project I need to use the ConfigurationManager. Please advise

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to access a connection string from a web.config or app.config in a class libr

    The problem is your windows app can't access the web.config file. You can try to create a web service to return the connection string (or any data that you would need for your windows app).
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How to access a connection string from a web.config or app.config in a class libr

    Thanks stanav. I've managed to fix it. I used the configuration manager class rather than the webconfigurationmanager one and it works for both

    Code:
    Configuration.ConfigurationManager.ConnectionStrings("constr").ConnectionString

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width