Reading appSettings and connectionStrings from web.config file in

Posted by in ASP.NET category on for Beginner level | Views : 56625 red flag

In this tutorials, I am going to explain how to read appSettings and connectionStrings values from the web.config file.
To do so, lets add one appSettings and connectionStrings into our web.config file like this


<add key="AppKey" value="Value in AppSetting key"/>
<add name="ConnStr" connectionString="server=localhost;database=mydatabase;uid=uid;pwd=pwd;"/>

In the code behind file, you will have to use System.Configuration namespace. So add it at the top of the .cs file like

using System.Configuration;

Now, write the following code to retrieve the values from the web.config appSettings and connectionStrings.

string str = "connectionStrings value: " + ConfigurationManager.ConnectionStrings["ConnStr"].ToString();

str += "<br />appSetting value: " + ConfigurationManager.AppSettings["AppKey"].ToString();
litText.Text = str;

Is that simple???

Write any feedback or question you have.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Raja Dutta
Member Level:
Member Status: Member
Member Since: 6/2/2008 12:47:48 AM
Country: United States
Regards, Raja, USA

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)