Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Reading connectionString from web.config file in asp.net

In this article, we are going to learn reading connectionString from web.config file in asp.net. 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 tag as shown in code below.



add name="MyConnectionString" connectionString= class='string'>"server=YourMachineName/IP;
database=YourDataBaseName; uid=DataBaseUserId; password=Password;"
providerName="System.Data.SqlClient"/>

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.

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.

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"]
.ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
}

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


This post first appeared on ASPArticles, please read the originial post: here

Share the post

Reading connectionString from web.config file in asp.net

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×