Problem with changing connection string that saved in app.config
-
Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks
-
Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks
Could you share the code snippets…So that we can get the actual scenario properly?
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
Could you share the code snippets…So that we can get the actual scenario properly?
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
here is my Settings.Designer.cs code (my modification code is bold) :
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
//[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")]
public string mehrConnectionString {
get {
return ((string)(this["mehrConnectionString"]));
}
set
{
this["mehrConnectionString"] = value;
}
}\[global::System.Configuration.**UserScopedSettingAttribute()**\] \[global::System.Diagnostics.DebuggerNonUserCodeAttribute()\] \[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)\] **//\[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")\]** public string mehrConnectionString1 { get { return ((string)(this\["mehrConnectionString1"\])); } **set { this\["mehrConnectionString1"\] = value; }** }
here is my code to save in app.config :
Properties.Settings.Default.serverName = this.txtServer.Text;
Properties.Settings.Default.userName = this.txtUser.Text;
Properties.Settings.Default.password = this.txtPass.Text;if (this.radioLocal.Checked) { Properties.Settings.Default.network = false; Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text); Properties.Settings.Default.mehrConnectionString1 = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text); } else { Properties.Settings.Default.network = true; Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;User Id={1};Password={2};", this.txtServer.Text, this.txtUser.Text, this.txtPass.Tex
-
here is my Settings.Designer.cs code (my modification code is bold) :
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
//[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")]
public string mehrConnectionString {
get {
return ((string)(this["mehrConnectionString"]));
}
set
{
this["mehrConnectionString"] = value;
}
}\[global::System.Configuration.**UserScopedSettingAttribute()**\] \[global::System.Diagnostics.DebuggerNonUserCodeAttribute()\] \[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)\] **//\[global::System.Configuration.DefaultSettingValueAttribute("Data Source=.;Initial Catalog=mehr;Integrated Security=True")\]** public string mehrConnectionString1 { get { return ((string)(this\["mehrConnectionString1"\])); } **set { this\["mehrConnectionString1"\] = value; }** }
here is my code to save in app.config :
Properties.Settings.Default.serverName = this.txtServer.Text;
Properties.Settings.Default.userName = this.txtUser.Text;
Properties.Settings.Default.password = this.txtPass.Text;if (this.radioLocal.Checked) { Properties.Settings.Default.network = false; Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text); Properties.Settings.Default.mehrConnectionString1 = string.Format("Data Source={0};Initial Catalog=mehr;Integrated Security=True", this.txtServer.Text); } else { Properties.Settings.Default.network = true; Properties.Settings.Default.mehrConnectionString = string.Format("Data Source={0};Initial Catalog=mehr;User Id={1};Password={2};", this.txtServer.Text, this.txtUser.Text, this.txtPass.Tex
Probably you are loading the app.config several times, why dont you use a Hashtable for managing multiple connectionstring Example: Hashtable ObjHashtable = new Hashtable(); ObjHashtable.Add("Key1", "Data Source= source_1 ;Initial Catalog= DatabaseName_1;User ID=id;Password=pwd;"); ObjHashtable.Add("Key2", "Data Source= source_2 ;Initial Catalog= DatabaseName_2;User ID=id;Password=pwd;"); You just need to load the Hashtable only one time........... public SqlDataReader Example(string strSql, string strKey) { ObjSqlConnection = new SqlConnection(ObjHashtable[strKey].ToString()); ........... ....... }
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
Probably you are loading the app.config several times, why dont you use a Hashtable for managing multiple connectionstring Example: Hashtable ObjHashtable = new Hashtable(); ObjHashtable.Add("Key1", "Data Source= source_1 ;Initial Catalog= DatabaseName_1;User ID=id;Password=pwd;"); ObjHashtable.Add("Key2", "Data Source= source_2 ;Initial Catalog= DatabaseName_2;User ID=id;Password=pwd;"); You just need to load the Hashtable only one time........... public SqlDataReader Example(string strSql, string strKey) { ObjSqlConnection = new SqlConnection(ObjHashtable[strKey].ToString()); ........... ....... }
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
Thanks Md. Marufuzzaman i want to save my connection string in app.config to save this state in future use. next time it must use the saved settings which user set.
You are most welcome.......:) Absolutely you save your connectionString in your app.config. My point of view was: 1. Read your config file 2. Fill the HasTable 3. Use the HasTable runtime so that you not need to invoke config file several times.
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
You are most welcome.......:) Absolutely you save your connectionString in your app.config. My point of view was: 1. Read your config file 2. Fill the HasTable 3. Use the HasTable runtime so that you not need to invoke config file several times.
Thanks Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
Hi i want to change connection string that saved in app.config (which created in visual studio wizard dialog). i changed some Settings.Designer.cs code which described in this thread[^] to make that editable. that's works good when i change it at runTime and load it again, but when i exit from my app and look at my saved properties, it's again backed to earlier settings (before my edit). what's the problem and how to solve this ? thanks