Combo box has two values Live and Backup, on selected index change code is:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataConnection.ConnectionString = ConfigurationManager.ConnectionStrings[comboBox1.SelectedItem.ToString()].ConnectionString;
label1.Text = DataConnection.ConnectionString;
}
I'm just printing connection string on my label where as you can use it to communicate with DB as and when required as it is assigned to your DataConnection.ConnectionString property which holds ConnectionString.
Add System.Configuration namespace to use ConfigurationManager class.
My app.config file:
<configuration>
<connectionstrings>
<add name="Live" connectionstring="LiveDB" />
<add name="Backup" connectionstring="BackupDB" />
</connectionstrings>
</configuration>
Now my DataConnection class is the class which is communicating with DB based on ConnectionString property value.
class DataConnection
{
public static string ConnectionString { get; set; }
}
Mark this as acceptable answer if it resolves your issue.
Thanks.