Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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 from the selectedvalue at cb1


This code below 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 next code is almost correct and also populating cb2 BUT with System.Data.DataView.
What's wrong?
I want to get in cb2 a specific column of the selected table in cb1

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(); // If i debug there are no records in there
sqlDataAdap.Fill(dt); 
comboBox1.DataSource = dt; 
} 
catch (Exception ex) 
{
 MessageBox.Show(ex.Message); 
} 
finally 
{ 
con.Close(); 
}
 
  }


Thnx
Posted
Comments
BillWoodruff 23-Jan-15 7:19am    
Stop reposting, and edit your original question.

Add this line

C#
comboBox1.DisplayMember = "Projektdefinition"; // add this line
comboBox1.ValueMember = "Projektdefinition";// add this line
comboBox1.DataSource = dt;
 
Share this answer
 
Comments
mikybrain1 23-Jan-15 9:13am    
It's still not fetching in the cb
mikybrain1 23-Jan-15 9:16am    
The values are in the DataTable if i debug BUT it not fetching in the cb
try with
C#
sqlCmd.CommandText = ("SELECT [Projektdefinition] FROM " + ((DataRowView)cbTbl.SelectedItem).Row["TABLE_NAME"].ToString()); 
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 try
{
con.Open();
DataTable datatable= new DataTable();
sqlDataAdap.Fill(datatable);
comboBox1.DataSource = datatable;
comboBox1.DisplayMember = "Projektdefinition";
comboBox1.ValueMember = "Projektdefinition";
}
catch (Exception ex)
{
 MessageBox.Show(ex.Message);
}
 
Share this answer
 
v4
Comments
mikybrain1 23-Jan-15 7:29am    
@ DamithSL
Thnx but it isn't working (:
DamithSL 23-Jan-15 7:31am    
there was space when given column name in Row["TABLE_NAME "], if you copy my code directly, please recheck and correct it.
mikybrain1 23-Jan-15 7:37am    
Still populating it with system.data.dataview
DamithSL 23-Jan-15 7:46am    
debug and check the value you get as cbTbl.SelectedValue and what is the value you get as ((DataRowView)cbTbl.SelectedItem).Row["TABLE_NAME"]
mikybrain1 23-Jan-15 7:58am    
HiAs i debugged i saw the values from the column (projektiondefinition)
BUT
the combobox1 is still dispaly "System.Data.Dataview" and i still don't know why although i added the two lines u suggested

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