Search
MVC Online Training
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Sat, 20-Sep-2014 Authors
Sheonarayan
250
All Time Authors
Sourav.Kayal
39750
Sheonarayan
38050
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads
 Article

Getting connectionStrings / appSettings values from web.config file

SheoNarayan
Posted by under ASP.NET category on for Beginner level | Views : 46607 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
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 !!!

Page copy protected against web site content infringement by Copyscape
About the Author

SheoNarayan

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Microsoft_MVP,Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com

Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan
Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)