How to combine two combobox in visual studio 2019

  • 268 Views
  • Last Post 13 February 2019
stharmini98@gmail.com posted this 12 February 2019

This is the coding I wrote to show the tables and columns in separate combo box in Visual Studio IDE. But its showing table names and column names in separate combo box but I couldn't link them regarding if I selected one table from the first combo box the columns should come in the second combo box.

{
                    con.Open();
                    SqlCommand sqlCmd = new SqlCommand();
                    sqlCmd.Connection = con;
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.CommandText = " SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'" +
                                         " ORDER BY TABLE_NAME";

                    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
                    DataTable dtRecord = new DataTable();
                    sqlDataAdap.Fill(dtRecord);
                    comboBox2.DataSource = dtRecord;
                    comboBox2.DisplayMember = "TABLE_NAME";
                    con.Close();
                }

                {
                    con.Open();
                    SqlCommand sqlCmd = new SqlCommand();
                    sqlCmd.Connection = con;
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.CommandText = " SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS" +
                                         " ORDER BY TABLE_NAME";

                    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
                    DataTable dtRecord = new DataTable();
                    sqlDataAdap.Fill(dtRecord);
                    comboBox1.DataSource = dtRecord;
                    comboBox1.DisplayMember = "COLUMN_NAME";
                    con.Close();

                }
java2s posted this 13 February 2019

I think you should add condition to the second sql query

sqlCmd.CommandText = " SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME="+ comboBox2.Text +" ORDER BY TABLE_NAME";

As the old query i can't see the filter

Close