Go to DotNetFunda.com
  Welcome, Guest!  
LoginLogin  
{ Submit resources and get monthly gifts !!! }
Submit: Article | Interview Question | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Reading appSettings and connectionStrings from web.config file in asp.net

All Articles | Post Articles

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

 Posted on: 10/9/2007 7:14:12 PM by Raja | Views: 1510 | Category: ASP.NET | Level: Beginner | Print Article
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


- Hide Code
<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.


- Hide Code
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.

Bookmark and Share

About Raja Dutta

Experience:3 year(s)
Home page:
Member since:Monday, June 02, 2008
Biography:--
Raja
 Latest post(s) from Raja

   ◘ Forms Authentication in ASP.NET with C# posted on 7/30/2008 4:08:34 PM
   ◘ Getting selected value from asp:RadioButtonList in JavaScript posted on 4/15/2008 4:03:49 AM
   ◘ Reading appSettings and connectionStrings from web.config file in asp.net posted on 10/9/2007 7:14:12 PM
   ◘ Generating Ms Word document in ASP.NET and C# posted on 9/11/2007 4:49:45 PM
   ◘ Dynamically writing Meta tags on Master page from a User Control or any other class file posted on 6/21/2007 2:29:26 PM


Question: Why to use www.dotnetfunda.com google search?
Answer: This search has been especially optimized to search technical articles. You may find to-the-point results in comparison with other search.
Google
About Us | Contact Us | Privacy Policy and Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
This site is best viewed with a resolution of 1280x720 (or higher) and Microsoft Internet Explorer 6.0+ or Firefox 2.0+.