Click here to Skip to main content
15,885,900 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I’ve two comboboxes which should contain two different informations.

1.cb1: select table_name from information_schema.tables (this display multiple tables)
2.cb2: should populate it with a column name.

Example: I've three tables in cb1 with the same attributes but have different values at the column EmpName (tblLondon,tblBerlin,tblRom,...)

Now I wanna display in second comboboxe the column EmpName dynamically whenever I choose a table in first combobox.

cb1                  cb2
[tblLondon]          [John,Mavis,Chris,Mike..] 
            
OR

cb1                    cb2
[tblBerlin]       [Günther,Peter, Sophie,Sunny, ..]


Can u plz help me out

This code is working and populating my cb1 (combobox).



C#
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        SqlConnection con = new SqlConnection(C);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC");
        try
        {
            // Open connection, Save the results in the DT and execute the spProc & fill it in the DT
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            dt = new DataTable();
            adapter.Fill(dt);
            cbTbl.DisplayMember = "TABLE_NAME";
            cbTbl.ValueMember = "TABLE_NAME";
            //Fill combobox with data in DT
            cbTbl.DataSource = dt;
            // Empty bzw. clear the combobox
            cbTbl.SelectedIndex = -1;



The code below is populating the combobox 1 BUT if System.Data.DataView.
What's wrong?

C#
private void comboBox1_SelectedIndexValue(object sender, EventArgs e)
        {
if (cbTbl.SelectedValue != null) 
{ 
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString; SqlConnection con = new SqlConnection(C); 
SqlCommand sqlCmd = new SqlCommand(); 
sqlCmd.Connection = con; 
sqlCmd.CommandText = (" SELECT [Projektdefinition] FROM " + cbTbl.SelectedValue.ToString() + ""); 
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 try 
{ 
con.Open(); 
dt = new DataTable(); 
sqlDataAdap.Fill(dt); 
comboBox1.DataSource = dt; 
} 
catch (Exception ex) 
{
 MessageBox.Show(ex.Message); 
} 
finally 
{ 
con.Close(); 
}

        }
Posted
Updated 22-Jan-15 23:09pm
v3

1 solution

Since you are using SqlCommand, when index changes do:

C#
int i = combo1.getSelectedIndex;
if(i>=0)
{
// get item value from combo1
//create sql query for that item
//fill combo 2 with results
}


Another, easier way is to create a DataSet and TableAdapter and bind the data to the comboboxes. You can do yourself or add a dataset to your project and add tables from your database. After building the dataset and adapters will be available to you in toolbox when in form designer.
From there you bind combo1 to one table and field and the second using the foreign key or a query which you create in dataset designer. Then any changes in combo1 will automatically update combo2.
 
Share this answer
 
Comments
mikybrain1 22-Jan-15 15:31pm    
Thnx
BillWoodruff 23-Jan-15 1:46am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900