Accessing the web.config from a DLL

asp.netSometimes your website (or a site someone else made)  will include a DLL that interface with your site.  In a recent project, I encountered an issue where the DLL needed to utilize the web.config to access the database. With a quick search, I encountered many others with the same problem. There was a few solutions, but none of them work out of the box. With a little tweaking I was able to make it work.

In the web.config we have our connectionString:

MyDBConnection" connectionString ="server=localhost;database=MyDB; user id=username; pwd=password"/>

And to access it from the DLL:

string connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyDBConnection"].ToString();

And finally what they all missed:

Right-Click on your DLL project and Add a Reference to System.Configuration

All solutions that I found said to simply add the ref to the page “using System.Configuration”, which did not work.