Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i need here first to load comboBox1 with data in database called Conductors from table called Condtype filed called Type
witch is successfully done

now i need another comboBox3 to be loaded but based on first comboBox1 selection based on first comboBox1 selected item to filter with it another table in same database called conductors different table called conductors filled called ssize and get crossbonding values in the new combobox3 but not working ??

What I have tried:

C#
connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb");
            command = new OleDbCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandText = "SELECT * FROM Condtype";
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr["Type"].ToString());

            }
            connection.Close();
                      
            connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb");
            command = new OleDbCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandText = "SELECT * FROM Conductors where Shape = '"+comboBox1.SelectedItem+"'";
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                comboBox3.Items.Add(dr["Ssize"].ToString());

            }
            connection.Close();
Posted
Updated 6-Jul-19 4:06am
v2

I'd suggest to use ComboBox1.SelectedIndexChanged Event (System.Windows.Forms) | Microsoft Docs[^] event to fill in ComboBox3. For example:

C#
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DataTable dt = new DataTable();
    string sQry = $"SELECT * FROM Conductors where Shape='{comboBox1.SelectedItem.ToString()}';";
    if (sQry=string.Empty) return;
    using(OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb"))
        {
            connection.Open();
            using(OleDbCommand command = new OleDbCommand(sQry, connection))
                using(OleDbDataReader reader = command.ExecuteReader())
                {
                     dt.Load(reader);
                     ComboBox3.DataSource = dt;
                     ComboBox3.DisplayMember = "Shape";
                     ComboBox3.ValueMember = "ShapeId";
                }
        }
}


For further details, please, see:
ComboBox.DataSource Property (System.Windows.Forms) | Microsoft Docs[^]
ListControl.DisplayMember Property (System.Windows.Forms) | Microsoft Docs[^]
ListControl.ValueMember Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
katkot_rewsh 6-Jul-19 4:55am    
Thanks Maciej for your solution

now i need to make it more complex using 3 combobox, can you please tell me where is the wrong in below code ?

string sQry = "SELECT * FROM Conductors where Type = ('"+ Convert.ToString(comboBox1.SelectedItem)+"') and Shape = ('" + Convert.ToString(comboBox2.SelectedItem) + "'),";
Maciej Los 7-Jul-19 6:17am    
Based on that piece of code - i can't.
 public partial class Form1 : Form
    {
        // خاص بالربط مع قاعدة البيانات

        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Conductors.accdb");
        OleDbCommand command;
        OleDbDataReader dr;
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // خاص بربط الكومبو بوكس مع الاكسس

            connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb");
            command = new OleDbCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandText = "SELECT * FROM Condtype";
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr["Shape"].ToString());

            }
            connection.Close();

            // خاص بربط الكومبو بوكس مع الاكسس

            connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb");
            command = new OleDbCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandText = "SELECT * FROM Condtype2";
            dr = command.ExecuteReader();
            while (dr.Read())
            {
                comboBox2.Items.Add(dr["Type"].ToString());

            }
            connection.Close();
        
    }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sQry = "SELECT * FROM Conductors where Shape = '" + comboBox1.SelectedItem + "' and Type = '" + comboBox2.SelectedItem + "'";
            using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Conductors.accdb"))
            {
                connection.Open();
                using (OleDbCommand command = new OleDbCommand(sQry, connection))
                using (OleDbDataReader reader = command.ExecuteReader())
                {
                    dt.Load(reader);
                    comboBox3.DataSource = dt;
                    comboBox3.DisplayMember = "Ssize";
                    comboBox3.ValueMember = "ID";
                }
            }
        }
    }
}
 
Share this answer
 
v2

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