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:

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

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.

And here we go!

I welcome myself to this blog, since I am the only one on here. Hopefully I can provide some inspiration and help to others with my Thoughts and Code.

Hopefully there will be enough exciting things in my life, that I can write something daily on here.

Enjoy.