1

In my windows forms application I have a scenario like this:

I have 5 checkboxes which awaits user input. When the user clicks a checkbox,my application should connect to a database. Clicking each checkbox should connect to different different databases. For simplicity I have given my ip addresses and all details in app config as

<add key="SQLServer1" value="mmm.mm.mm.m,abcd" />
<add key="SQLServer2" value="nnn.nn.nn.n,wxyz" />

I have written the code like this:

ConnectionString = "Data Source=" + ***** + ";Initial Catalog=" + Master + ";Persist Security Info=True;Connect Timeout=0;User ID=" + ConfigurationSettings.AppSettings["username"] + ";Password=" + ConfigurationSettings.AppSettings["password"];

I should get help from you people where I marked *. What I meant by * is that my application should be able to connect with relevant ip based on what the user clicked..

Is there any chance for what I said?

Any other alternatives are also really appreciated..

5
  • 1
    You can declare a variable and use if statement, then based on your checkbox change the variable(read from config and assign to variable)... Commented Sep 17, 2013 at 10:31
  • @Sam:But as I said, if I have 5 checkboxes, then I should have 5 if conditions..right? Commented Sep 17, 2013 at 10:32
  • Yes that is right or use switch statement Commented Sep 17, 2013 at 10:33
  • yes. you must have conditions to do that Commented Sep 17, 2013 at 10:33
  • I'm not sure this is of great help to you, but I'm guessing you could simply use the Tag property of the checkboxes. Write the key "SQLServer?" in that Tag property, in the Visual designer and in whatever CheckBox event handler you had in mind just do something like: ConnectionString = "Data Source=" + ConfigurationManager.AppSettings[ (sender as Control).Tag.ToString() ] + ";Initial Catalog=" + Master + ..... Commented Sep 17, 2013 at 10:36

5 Answers 5

1

How are you connecting to the database? Via Entity Framework, or dbcontext, or custom SQLConnection classes?

One thing you can do , as soon as user selects a checkbox, get the value of connection string and server Ip from web.config as

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[VARIABLENAME];

and the app setting.

Replace the ** by app setting value and pass the connectionstring to the database connection class.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use formatted variables like shown below:

string connectionString = String.Format("Data Source={0};Initial Catalog=" + Master + ";Persist Security Info=True;Connect Timeout=0;User ID=" + ConfigurationSettings.AppSettings["username"] + ";Password=" + ConfigurationSettings.AppSettings["password"], <get value from checkbox>);

Comments

0

Each checkBox has a checkBox.Checked parameter, so you can make a function that will insert the proper IP address when a checkBox is checked:

if(checkBox1.Checked) { string DataSource = some IP address; }

and you repeat this code for each pair of checkBox/IP address you have.

P.S since you probably want to connect to only one database in one moment, you should use radioButtons instead of checkBoxes

Comments

0

i don't know why you use CheckBox. if you need yo be connected to only one DB you should use RadioBox. if anyway you need several DB you can registed the checkBox Checked event:

     this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);

and do:

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  {
     if (checkBox1.Checked)
     {
        string currStr = ConnectionString.Replace("*****", currIP);// the currIP is up to u  
     }
     else
     {

     }

  }

now you can put them all in a list

List<string> connStrings = new List<string>();

and do

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  {
     string currStr = ConnectionString.Replace("*****", currIP);// the currIP is up to u  

     if (checkBox1.Checked)
     {
        connStrings.Add(currStr);
     }
     else
     {
        connStrings.Remove(currStr);
     }

  }

now that you got them all in a list you can use it with a foreach

foreach(string curr in connStrings)
{
}

i do remind that you'll have to register each CheckBox to his own event method

Comments

0

I had a scenario where i had to use 5 different databases in one application.

What is did is, i had a single class DBUtility which provides all database related functionality. in this class i had a method called init() which initializes my connection string.

so i created other 4 classes which inherited from the main class DBUtility and override the init() method to different connection strings.

You can also create similar structure and then based on checkbox selection create object of required class

e.g.

DBUtility obj = new DBUtility2();

where

public class DBUtility
{
public SqlConnection cn;
 public void init()
{
//initialize cn here
}

public DataSet getdata(string query)
{
//execute query using cn
}
}

public class DBUtility2 : DBUtility
{
public void init()
{
//initialize cn here
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.