ASP.NET - configuring web.config - Asked By bhanupratap singh on 08-Feb-12 01:47 AM

How to configure Connection string in web.config file
thnaks
Suchit shah replied to bhanupratap singh on 08-Feb-12 01:59 AM
To Configure connection string in web.config file you can do it in following way

<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=djdesai;Initial Catalog=CGM_ATR_TEST;User ID=sa;Password=matrix3;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
    <add name="CGM_ATR_TESTConnectionString" connectionString="Data Source=djdesai;Initial Catalog=CGM_ATR_TEST;User ID=sa;Password=matrix3;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>


Here in this mainly you required to take care of 4 main thing

1) Datasource = name of your sql server instance
2) Initial Catalog = Your database name on that sql server instance
3) username = username to connect the DB
4) password = passwrod to connect that sql server instance

Hope your doubt get clear
DL M replied to bhanupratap singh on 08-Feb-12 02:25 AM
Hi..

There are two way connection strings stored in the web.config or machine.config file. http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx

& also show step by step http://peterkellner.net/2008/02/23/webconfigbestpractice/
dipa ahuja replied to bhanupratap singh on 08-Feb-12 02:33 AM
<add name="ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
 
Use in .cs:
string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;