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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

You can pass an array of integer to an ASP.NET Web Web API REST service by setting the [FromUri] attribute...
One of the common ways to refresh webpage every few mins is by using JavaScript or HTML Meta tag. For...
This post will provide the list of some of the top ASP.NET based Content Management Systems (CMS) that you may...