Read Connection String from Web.Config file in ASP.Net using C# and VB.Net

In this post, we will learn about how to Read Connection String from Web.Config file in ASP.Net using C# and VB.Net with an example.

Description:

In my previous posts I have explained various topics like Why is Dictionary preferred over Hashtable, Introduction to BackboneJS, Bind an enumeration to combobox in WinForm, jQuery is undefined error: Common mistake every developer should avoid, 15 Powerful jQuery Tips and Tricks for Developers and Designer.

Now in this post, I will explain how to Read Connection String from Web.Config file in ASP.Net using C# and VB.Net with appropriate example. In ASP.Net Web applications, it requires reference to the System.Configuration Assembly in order to read Connection String value from the Web.Config file.

First, 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=DotNetK-PC\SQL2005;Initial Catalog=Northwind;Integrated Security=true" />
    </connectionStrings>
</configuration>

Now,you can read the Connection String value by its name from the Web.Config file in the following way.

C# :

string connectionStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

VB.Net :

Dim connectionStr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString

Read :

Summary

You can also read about ASP.NET, C#.Net, JQUERY, AJAX, JavaScript
I hope you get an idea about how to Read Connection String from Web.Config file in ASP.Net using C# and VB.Net.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you liked this post, don’t forget to share this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website