none
Connection between comboboxes RRS feed

  • Question

  • Hello, 

      I am new in C# and I have the follow problem: I have created a database as follow: Table1(Key1, Name), Table2(Key2, Name), Table3(Key1, Key2). I have a windows form application with 2 comboboxes. I have already load my first combobox with the "Name" of Table1 as follow:

     private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(@"");
                conn.Open();
                SqlCommand sc = new SqlCommand("select Key1, Name from Table1", conn);
                SqlDataReader reader;
    
                reader = sc.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("Key1", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Load(reader);
    
                comboBox3.ValueMember = "Key1";
                comboBox3.DisplayMember = "Name";
                comboBox3.DataSource = dt;
    
                conn.Close();
            }
    Then I tried to fill my second combobox by using the sql phrase "SqlCommand cmd = new SqlCommand("Select Table2.Name from Table1, Table2,  Table3 where temp='" + comboBox3.SelectedItem + "'", conn);"

    That I don't know is how I will do something like that? Is it right to use the same code in again? And how I will appear and save the "temp" value?

    Thank you.

    Saturday, November 8, 2014 3:56 PM

Answers

  • Sorry for the slow reply, Kelly. I hope this helps.

    You can use the comboBox3.SelectedValue in your SQL query, since you've specified Key1 as the ValueMember. Your SqlCommand could look something like this (notice that I'm using string.Format() to get the query string ... it's easier to read and easier to use:

    string QueryString = string.Format("SELECT Name FROM Table2 JOIN Table3 ON Table3.Key2 = Table2.Key2 AND Table3.Key1 = '{0}'", comboBox3.SelectedValue);
    SqlCommand cmd = new SqlCommand(QueryString, conn);
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, November 12, 2014 6:14 AM

All replies

  • Hi Kelly,

    I'm not sure what you want to query with the second SqlCommand. What is temp and where did it come from? It's not in the code you showed, so perhaps it's a column in one of your database tables? . Are you trying to JOIN Table1, Table2 and Table3? If so, you'd need to use the JOIN keyword in your SQL query. Without additional feedback as to what you need to do with that second query, I hesitate to give you examples that might be confusing.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, November 9, 2014 5:43 PM
  • Hello, 

      Thank you for your message.

       I have already load the "Name" from Table 1 in my first combobox. I am trying according the checked value at the first combobox to load the "Name" from Table 2 at the second combobox. The "temp" is a temporary variable that I use.

    Monday, November 10, 2014 6:14 PM
  • Sorry for the slow reply, Kelly. I hope this helps.

    You can use the comboBox3.SelectedValue in your SQL query, since you've specified Key1 as the ValueMember. Your SqlCommand could look something like this (notice that I'm using string.Format() to get the query string ... it's easier to read and easier to use:

    string QueryString = string.Format("SELECT Name FROM Table2 JOIN Table3 ON Table3.Key2 = Table2.Key2 AND Table3.Key1 = '{0}'", comboBox3.SelectedValue);
    SqlCommand cmd = new SqlCommand(QueryString, conn);
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, November 12, 2014 6:14 AM