How to get or read connection string from web .config file



In this article i will explain How to Get or read connection string from web.config file.


In the Previous articles i explained how to integrate ckeditor with asp.nethow to upload image to the server or application using Ckeditor , how to change or set width of ckeditor textbox area using asp.nethow to set or change height of Ckeditor textbox area using ASP.Netintegrate custom image uploader control with ckeditor using asp.nethow to add membership tables to the existing database, where to add connection string in web .config file of  and many more about Asp.Net.


To read the connection string into your code, use the ConfigurationManager class.

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Remember to add reference to the System.Configuration component. Then include the namespace ("using System.Configuration;" in C# and "imports System.Configuration" in VB.Net) in your code to get access to the ConfigurationManager class.

Below is the sample Code using C#.Net to read connection string from web.config file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


        string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

        SqlConnection conn = new SqlConnection(connStr);


        SqlCommand command = new SqlCommand("SELECT CategoryName FROM Category", conn);

        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())





Give your Valuable Comments.
comments powered by Disqus
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
More Articles