Personal Blog of Mustafa Basgun

Posts Tagged ‘connection string

Getting Connection String From Web.Config Inside A DLL Code

leave a comment »

I have been working on creating some .NET dlls at work which would commonly be utilized by our web applications. And, one of the requirements was getting the connection string information from web.config file. Simply making the call by using the ConfigurationManager class wouldn’t work since it was giving some kind of compile time error when you try to build the dll. So, I needed to find a workaround to retrieve the connection string from either connectionStrings or appSettings elements. Here is the solution that I have figured out so far.

Necessary namespaces:


Generic function that grabs the information based on your connection string name:


Sample usage:


To understand the ConnectionStringSettingsCollection and NameValueCollection classes, followings links would be helpful:

Written by Mustafa Basgun

March 22, 2009 at 7:20 AM