Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 How to access a connection string from a web.config or app.config in a class library

$
0
0
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

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>