You can't change the connection string in the config, you either have to have all possible connections in the config and choose which one to use in your code, or create the connection dynamically
<connectionStrings>
<add name="MyCon1" connectionString="server=.\SQL2008; database=DB1; Trusted_Connection=true;"/>
<add name="MyCon2" connectionString="server=.\SQL2008; database=DB2; Trusted_Connection=true;"/>
<add name="MyCon3" connectionString="server=.\SQL2008; database={0}; uid={1}; pwd={2};"/>
</connectionStrings>
SqlConnection con = null;
if (someCondition)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon1"].ConnectionString);
}
else
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon2"].ConnectionString);
}
string dbToUse = "mydb";
con = new SqlConnection(string.Format("server=.;database={0};", dbToUse));
string username = "me";
string password = "mypassword";
con = new SqlConnection(string.Format(ConfigurationManager.ConnectionStrings["MyCon3"].ConnectionString, dbToUse, username, password));