Search This Blog

Friday 10 July 2015

Read connection from web.config c# or How to get or read connection string from web .config file in asp.net


Step 1:- web.config file

<?xml version="1.0" encoding="utf-8"?>

<configuration>
<connectionStrings>
  
<add name="ConnectionStr" connectionString="server=Server Name; database=Database Name; uid=User Id; pwd=Password;" providerName="System.Data.SqlClient" />

Or

<add name="ConnectionStr" connectionString="Data Source=Server Name; Initial Catalog=Database Name; User ID=User Id; Password=Password;    providerName="System.Data.SqlClient" />

</connectionStrings>

<appSettings>

<add key="ConnectionStr" value="server=Server Name; database=Database Name; uid=User Id; pwd=Password;"/>

Or

<add key="ConnectionStr" value="Data Source=Server Name; Initial Catalog=Database Name; User ID=User Id; Password=Password;"/>

</appSettings>
</configuration>


Step 2:- Get or Read connection string in code behind

protected void Button1_Click(object sender, EventArgs e)
{
                string connectionStr = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
                SqlConnection sqlConnection = new SqlConnection(connectionStr);
                sqlConnection.Open();

Or

                string connectionStr = ConfigurationManager.AppSettings["ConnectionStr"];
                SqlConnection sqlConnection = new SqlConnection(connectionStr);
                sqlConnection.Open();
}

Step 3:- Explanation

You can store database connection strings either in <connectionStrings> element or <appSettings> element.
Apart from you can stores custom application configuration information in <appSettings> element i.e.  File paths, XML Web service URLs, or any information stored in an application's .ini file.

If you are using the <connectionStrings> element to store the connection string in web.config file so, you can use the ConfigurationManager.ConnectionStrings class to access.


                string connectionStr = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
                SqlConnection sqlConnection = new SqlConnection(connectionStr);
                sqlConnection.Open();

If you are using the <appSettingselement to store the connection string in web.config file so, you can use the ConfigurationManager.AppSettings class to access.

                string connectionStr = ConfigurationManager.AppSettings["ConnectionStr"];
                SqlConnection sqlConnection = new SqlConnection(connectionStr);
                sqlConnection.Open();

Note:-

Add the namespaces in code behind.

using System.Configuration;

If you are not getting System.Configuration; namespace in code behind, Follow below steps to add.

Microsoft Visual Studio 2012

Step 1: Right click References folder in your project and click on Add References...
Step 2: Left Pane, Select Assemblies, Select Framework
Step 3: Middle Pane Check the System.configuration check box
Step 4: Click OK

Now System.configuration has added to References folder.

No comments:

Post a Comment