ConnectionStrings or appSettings in Asp.net

ConnectionStrings or appSettings in Asp.net

 Though you have shifted to ASP.net 2.0 or later then why in your application you are still storing connection Strings in AppSettings ?

Here is sample code to access connectionStrings.

In web.config

<connectionStrings>

<add connectionString="servr=.;uid=sa;password=pass;database=mydb" name="XYZConnectionString" providerName="System.Data.SqlClient"/>

</connectionStrings>

In code behind you can access it like this.

string connection = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;

Benifit of adding connectionString in connectionString section is, you will be able to configure your ASP.net membership provider , custom provider and third party components.

To know which connction string to use for what DB provider, you can refer www.connectionstrings.com

Note*: If you are using Class Library project in your ASP.net project and your library project needs to access connection string,
          you need to add refrence of System.Configuration in your class library project.

Satalaj.
 

 

Tags:

ASp.net 2.0 step by step Membership Provider asp net image uploadAsp.net Interview Questions for 2 years or 3 years experianceASP.net File HandlingWhat is web garden in Asp.net?Asp.net Convert String To ImageAsp.net Convert Image To Base64Asp.net open new windowHack asp.netAsp.net How To Write a Cookie and ReadListBox Asp.net Binding csharp

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.