Read ConnnectionString from web.config in C#

ConnectionStringis normally stored in web.config and it is recommended to store connectionstring in web.config file. To use connectionstring in our application we need to read connectionstring from web.config.

We need to use ConfigurationManager class in order to read connectionstring from web.config. You may need to add reference for assembly System.Configurationbefore using ConfigurationManager class.

string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);

Web.Config

<connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="YourConnectionStringHere" />

</connectionStrings>

Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net