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

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

You May Also Like

When you deploy your ASP.NET Web Application on Windows Server running IIS , there are times that you might receive...
This post will explain how to resolve the error “Handler “aspNetCore” has a bad module “AspNetCoreModuleV2” in its module list”...
This post will provide the list of some of the top ASP.NET based Content Management Systems (CMS) that you may...