How to Create Connection String in Asp.Net Web Config File

Introduction
Here, I will explain how to create connection string in asp.net web config file, to connect with database.

Description
First we have to add <connectionstring> tab inside a <configuration> tab in web config file.
Add following code in web config file


 <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=SHEKHAR-PC;Initial Catalog=TEMP;Persist Security Info=True;User ID=sa;Password=trucom@123"
      providerName="System.Data.SqlClient" />
  </connectionStrings>


Note: You have to change connectionString values, those are highlighted, as per your database credentials.

Now, we will call this connection string in aspx code behind page.For this we have to include System.Configuration namespace and place the following code into code behind.


include:
using System.Configuration;

and

string strcon = ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;

No comments:

Post a Comment