Monday, 9 May 2016

Reading connectionString from web.config file in ASP.Net with C#

In this article, we are going to learn reading connectionString from web.config file in So, to retrieve connectionstring from web.config file, first we need to define connectionString property within web.config file.

In Web.Config File

Write your database connection string in web.config file within <connectionStrings> tag as shown below in code.

  <add name="MyConnectionString" connectionString="server=YourMachineName/IP;
  database=YourDataBaseName; uid=DataBaseUserId; password=Password;" 

And also, we need to add the System.Configuration dll to project. Below is step to add System.Configuration dll.

Step 1: Write click on references and click on add reference as shown below.

add System.Configuration reference1

Step 2: Now the new window will open as show below. Go to Framework scroll down to System.Configuration and check to add and click on OK.

add System.Configuration reference2

Step 3: Rebuild your application.

Step 4: Now, go to your Code-Behind file and include the below Namespace.

using System.Configuration;

Step 5: Below is the code snippet to read the connectionstring from web.config file using c#.

string MyConnString = ConfigurationManager.ConnectionStrings["MyConnectionString"]

protected void Page_Load(object sender, EventArgs e)

In the next article, we are going to learn Reading appSettings from web.config file in



Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive