user3107343
user3107343

Reputation: 2299

How to set connection string by using combobox selected item?

I have two public connnectionstrings.I think this method is not usefull.I want user will choose from combobox before click button for connect to database.

public partial class Form1 : Form
{
    public string CompanyA = "Data Source=.;Initial Catalog=compA;User ID=sa;Password=**";
    public string CompanyB = "Data Source=.;Initial Catalog=compB;User ID=sa;Password=***";

    public Form1()
    {
        InitializeComponent();
    }
}

I used CompanyA and CompanyB strings in my codes.

using (SqlConnection con = new SqlConnection(CompanyA ))
{
    // do somethings....
}

using (SqlConnection con = new SqlConnection(CompanyB))
{
    // do somethings....
}

How can I choose connection string by comboboxes like this ? (sorry for wrong schema)

private void ConnectButton_Click(object sender, EventArgs e) 
{
    if ( (comboBox1.SelectedItem=="compA") && ( (comboBox2.SelectedItem=="compB") )
    {
        // public string CompanyA = "Data Source=.;Initial Catalog=compA;User ID=sa;Password=**";
        // public string CompanyB = "Data Source=.;Initial Catalog=compB;User ID=sa;Password=**";
    }
}

Upvotes: 0

Views: 1111

Answers (2)

drew_w
drew_w

Reputation: 10430

You can put whatever you want into a combobox by overriding the "ToString" method on the class. A simple example:

class ComboItemExample {
   public string DisplayString { get; set; }
   public string ConnectionString { get; set; }

   public override string ToString() { return DisplayString; }
}

Once you have this defined you can instantiate a few and add them to the combo box. That code looks like:

private string currentConnection = "<default connection>";

public Form1() {
   InitializeComponent();

   var firstConnection = new ComboItemExample { DisplayString = "Local Database", ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" };
   myComboBox.Items.Add(firstConnection);

   var secondConnection = new ComboItemExample { DisplayString = "Other Database", ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" };
   myComboBox.Items.Add(secondConnection);
}

Finally you want to use these objects when the user changes the selected item. That code looks something like:

public void myComboBox_SelectedIndexChanged(object sender, EventArgs e) {
  if (myComboBox.SelectedIndex <= 0) return;
  var newConnection = ((ComboItemExample)myComboBox.Items[myComboBox.SelectedIndex]).ConnectionString;

  // now use "newConnection" as your connection string. Save in a local member
  //    or pass directly into the call to create the database connection.
  currentConnection = newConnection;
}

That should do it! I hope this helps!

Clarification/Edit

To clarify, you can create the connection later with this code:

using (var connection = new SqlConnection(currentConnection)) {
   // use the connection here
}

I also added the a module variable in the above code to make it clear where to declare and set the current connection string. Best of luck!

Upvotes: 1

Muhammad Nour
Muhammad Nour

Reputation: 2327

You can do a connectionstring check

if (this.comboBox1.SelectedText == "compA")
{
    // connect using the companyA connectionstring
}
else
{
    // connect using the companyB connectionstring
}

Upvotes: 0

Related Questions