difference between writing connection string in web.config
through connection string and appsettings

Answer Posted / ramesh p

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.

Is This Answer Correct ?    27 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

We Only Know The Total Number Of Feet In The Farmyard. Write A Program that will compute the total number of rabbits and chickens in the farmyard. Assume number of feet in the farmyard are 40. how many rabbits and chickens are?

675


What are client activated objects and server activated objects?

719


What is server redirect?

734


what is AutoEventWireUp and what is the use of This property explain in details?

1355


What is route in web api?

734


What is side-by-side execution? Can two applications, one using a private assembly and other using a shared assembly, be stated as side-by-side executables?

783


What is the basic difference between asp and asp.net?

744


what are the events raised in asp.net page life cycle?in which stage view state can be loaded?

755


How to improve performance of web application asp.net mvc? : Asp.Net MVC

736


What is the purpose of url encoding?

704


What are the two types of web pages?

710


Who can consume WebAPI?

763


What are tuples?

751


What is difference between cache and session?

696


What is user control in asp.net?

732