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

What are triggers of an updatepanel?

744


What are strong names?

803


How do cookies work? Give an example of their abuse.

757


I have an excel file with data, i am importing this excel file data into Sqlserver 2005 database. while importing i am getting wrong data(ie, special characters) in one column(description column), upto some limit the data in that column is coming fine,after that data is coming like below. The "Walter" leather storage bench is one of our most popular styles. The top with the tufting and double stitching finish a very impressive piece. The size is perfect in front of beds and the storage adds another functional bonus. Open it up and sneak al���Ƴ�Â��ƴ�Ã��ƶ�Å�� al���Ƴ�Â��ƴ�Ã��ƶ�Å�� Is there anywhy to resolve this? (I am using recordset in coding for developing import process.) please help me soon.

1688


In Data grid the question is below quantity price total these are 3 fields available in data grid if you enter quantity the total has to update automatically.Price field is already filled completely

1786


What is custom events?

793


What are the advantages of the code-behind feature?

750


Explain the path instructions in xaml?

783


How many types of cache are there?

734


When does a session actually start?

786


Explain about ASP.NET?

845


Why asp.net is used?

765


What is parse in asp.net?

770


What’s difference between “optimistic” and “pessimistic” locking?

818


How can we identify that the Page is Post Back?

753