Home > Code > ASP.NET > How to configure and read connection string in web.config file?

How to configure and read connection string in web.config file?

by Thamil   on Jun 25, 2013   Category: ASP.NET   |  Views: 1119    |  Points: 25   |  Gold 


Storing connectionString in web.config file is a good practice instead of hard code in c# file. Here I’m using .net 3.5 for this sample.

<connectionStrings>
<add name="ConnectionString"
connectionString="Data Source=TEST-PC\SQLEXPRESS;Initial Catalog= TEST_DB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>


Here we need to use connectionString section instead of appsettings as like in .net 2.0 also see the below how to read this connection string in code file while opening the connection. For this we need to add/include the reference System.Configurence namespace.

string Con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


I hope this will help you how to configure and read connection string in web.config file


Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts