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 > Getting connectionStrings / appSettings values from web.config file

All Articles | Post Articles

Getting connectionStrings / appSettings values from web.config file

 Posted on: 6/3/2008 5:52:37 AM by SheoNarayan | Views: 2090 | Category: ASP.NET | Level: Beginner | Print Article
In this article, I am going to show how to get connectionStrings / appSettings values from web.config file. This article also contains how to get get the connectionString value at .aspx page in case you are using DataSource control like SqlDataSource.
Introduction

In many cases, we need to use some data string throughout the application, database connection string is the best example of it. Instead of writing the connection string wherever we are creating a connection, its good practice (and easy to maintain too) to store it into web.config file and get it at desired place.

 

Places to store data into Web.Config file

There are two places where we can store data into our web.config file. These are appSettings and connectionStrings. Following is the code snippet from the web.config file where we can store data and retrieve at later point of time.

<configuration>

<appSettings>

<add key="ConnStr" value="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyData\App_Data\Database.mdf;Integrated Security=True;User Instance=True"/>

</appSettings>

<connectionStrings>

<add name="ConnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyData\App_Data\Database.mdf;Integrated Security=True;User Instance=True"/>

</connectionStrings>

</configuration>

 

Here appSettings is meant for any data string that can be stored while connectionString is meant for storing the database connection strings only.

 

How to Get data from Web.Config

Getting data from web.config file is simple. If you want to get data from appSettings tag then you need to write following code

string connStr = System.Configuration.ConfigurationManager.AppSettings["ConnStr"].ToString();

To get data from web.config file stored under connectionStrings tag, you need to write following code

string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString();

 
Getting connectionStrings value into .aspx page

If for some reason you want to access the string stored in connectionStrings tag into .aspx page (You may need this while using SqlDataSource control), you need to write following code (Notice the code in the pink color <%$ ConnectionStrings:ConnStr %>).

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'

SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]" DataSourceMode="DataSet">

</asp:SqlDataSource>

 
Conclusion

Web.Config file is the best place to store small and simple string that can be used throught the application. Using System.Configuration.ConfigurationManager class that exists in System.Configuration namespace, we can retrive them wherever we need.

Thanks and Happy Coding !!!



Bookmark and Share

About Sheo Narayan

Experience:6 year(s)
Home page:http://sheonarayan.blogspot.com/
Member since:Tuesday, July 08, 2008
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, BCA, ADCA, MCA
Locations: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ SEO friendly pagination using asp:DataPager control posted on 8/18/2008 9:54:23 AM
   ◘ ListView Tips and Tricks posted on 8/14/2008 5:43:53 AM
   ◘ Working with HttpHandler in IIS 7 posted on 7/28/2008 3:56:22 PM
   ◘ Service Oriented Architecture, a real world example in ASP.NET with C# posted on 7/27/2008 1:20:50 PM
   ◘ Difference between asp:LinkButton, asp:ImageButton, asp:Button and asp:HyperLink control posted on 7/15/2008 9:25:51 AM


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+.