Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# connection string in web.config file

In my code behind page, how do I access the connection string which is stored in my web.config file?

like image 231
flavour404 Avatar asked Jun 25 '09 02:06

flavour404


3 Answers

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["YouConnStringName"].ConnectionString;

This requires references to System.Configuration.dll and System.Web.dll.

like image 128
Kyle Sonaty Avatar answered Oct 06 '22 09:10

Kyle Sonaty


How to: Read Connection Strings from the Web.config File

like image 35
Spig Avatar answered Oct 06 '22 07:10

Spig


From comment on http://msdn.microsoft.com/en-us/library/ms178411.aspx

string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myconnectionstringname"].ConnectionString;

Requires your project to reference to System.Web and System.Configuration. I had to actually add a reference to "System.Configuration" not just add a Using.

like image 23
Matthew Lock Avatar answered Oct 06 '22 08:10

Matthew Lock