views:

150

answers:

2

I have a class library that contains a valid connectionString inside the app.config. Inside that class library I want to use it with

ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

My ASP.net 4.0 framework application references that DDL and retrieves data from it. I want create a Entity Framework 4 DataContext within my DDL with the ConnectionString from the App.config. (I do not want to pass the connectionString from my ASP.net application in every single method. (I'm using ObjectDataSources))

However, this line inside my DLL throws a NullReferenceException.

ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

How can I fix this issue?

+3  A: 

I have a class library that contains a valid connectionString inside the app.config

A class library doesn't have an app.config file associated. It's the application consuming this assembly that does. So you need to put the connection string inside this config file (if this is an ASP.NET application this would be web.config). Thus adding an App.config file in a project of type class library in Visual Studio makes no sense.

Darin Dimitrov
Ok I understand, thanks. But why is the app.config autogenerated?
citronas
@citronas - It has other uses (*that* project doesn't know where it's doing, possibly in *other* solutions), unit testing the library for example.
Nick Craver
+2  A: 

In this case you put the same <connectionStrings> entry (the <add> in question) in your web app's web.config, ConfigurationManager.ConnectionStrings always looks at the current config, that's a web.config in your case.

Nick Craver