Reading appSettings and connectionStrings from web.config file in asp.net

Raja
Posted by in ASP.NET category on for Beginner level | Views : 57011 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

<appSettings>

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


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

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

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)