Dynamically Getting Connection String from Web.Config using ASP.NET C#

In this blog we are going to see, How to get connection string from web.config using asp.net c#.

Step 1:              Used Namespace

using System.Web.Configuration;

Step 2:              Connection String in Web.config

                <configuration>

  <add name="KeyName"

       connectionString="Data Source=ServerName;Initial Catalog=dbsample;Integrated Security=True" />

</configuration>

Step 3:              Usage

            GetConnectionStringFromWebConfigByName(“KeyName");

Step 4:              Method to fetch Connection String from  Web.config

public static string GetConnectionStringFromWebConfigByName(string name)

{

return WebConfigurationManager.ConnectionStrings[name].ConnectionString;

}


Thanks for reading this article. Have a nice day.

Ebook Download
View all
Learn
View all