In this article I will explain with an example, how to read or write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net.
The Connection String will be read and written from the Connection String from the ConnectionStrings section of the Web.Config.
 
 
Adding ConnectionString to the Web.Config file
You need to add the Connection String in the ConnectionStrings section of the Web.Config file in the following way.
<configuration>
    <connectionStrings>
        <add name="ConString" connectionString="Data Source=AyoSoftech-PC\SQL2012;Initial Catalog=Northwind;Integrated Security=true" />
    </connectionStrings>
</configuration>
 
 
Adding System.Configuration reference
The very first thing you need to do is to add reference of the System.Configuration Assembly to the project in the following way.
1. Right click on the project and click Add Reference option from the Context Menu.
Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net
 
2. From the Add Reference dialog box, click on .Net Tab and look for System.Configuration assembly. Once you find it simply select and click OK.
Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net
 
3. Once the reference is added, it will be shown in the References folder of the Solution Explorer.
Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Configuration;
 

 
Reading ConnectionString from Web.Config file in ASP.Net using C# and VB.Net
Once the reference is added, you can read the Connection String value from the Web.Config file in the following way.
C#
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;