Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / bathirasamy

both section are used for describing database strings and
values .This is used throughout our project if we using web
applications.

Is This Answer Correct ?    9 Yes 3 No

Post New Answer

More ASP.NET Interview Questions

Which is better session or viewstate?

0 Answers  


what is loosely coupled solution? How it can be used?

0 Answers   Siebel,


How to call a child form method from the master page?

1 Answers  


Explain the difference between codebehind="mycode.aspx.cs" and src="mycode.aspx.cs"?

0 Answers  


Whta are the Various steps taken to optimize a web based application (caching, stored procedure etc.) ?

0 Answers  


What is custom tag in Web.Config?

0 Answers   Accenture,


What is the differences between a primary key and a unique key in sql server?

0 Answers  


What’s the catch?

0 Answers  


How does the browser enable AutoPostBack functionality?

2 Answers  


How do you do exception management

1 Answers   Infosys,


Define the types of configuration files.

0 Answers  


What is difference between datalist and gridview?

0 Answers  


Categories