How to Read connection string from web.config in ASP.NET?

You can read connection string from web.config file in ASP.NET by using the ConfigurationManager class in case you want to read it in to a property in your .NET class library.

How to Read connection string from web.config in ASP.NET?

  1. Ensure that you add System.Configuration as a reference in your Visual Studio Solution.
  2. In you C# code, where you want to read the connection string from the config , use the below code snippet.
string conn = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

The variable conn should now have the connection string that is read from your web.config.

Copy link
Powered by Social Snap