Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a table named Item in sql server database. Item table definition is

SQL
Item_Code  Item_Name  Item_Make Item_Price UnitofMeasurement

           Cable        anchor  45.0000       meter
           Cable        polycab 30.0000       meter
           Button       anchor  15.0000       unit
           Button       havells 20.0000       unit
           Switch       cona    70.0000       unit


I have 2 combobox, 1st Combobox for Item_Name and 2nd for Item_Make. I want 2nd combobox to be filled with Item_Make on 1st Combobox selected Index changed. Unfortunately I was unable to achieve this.

What i have tried :

C#
private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBoxName.Text))
            {
                fillMake();
                comboBoxMake.SelectedIndex = -1;
            }           
            
        }

       private void fillName()
       {
           string str = "Select distinct Item_Name from Item";
           using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
           {
               using (SqlCommand cmd = new SqlCommand(str, con))
               {
                   using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                   {
                       DataTable dtItem = new DataTable();
                       adp.Fill(dtItem);
                       
                       comboBoxName.DataSource = dtItem;
                       comboBoxName.DisplayMember = "Item_Name";
                       comboBoxName.ValueMember = "Item_Name";
                   }
               }
           }
       }
       private void fillMake()
       {
            using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
            {
                string str = "Select Item_Make from Item Where Item_Name=@Item_Name";
                using (SqlCommand cmd = new SqlCommand(str, con))
                {
                    cmd.Parameters.AddWithValue("@Item_Name", comboBoxName.Text);
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        DataTable dtItem = new DataTable();
                        adp.Fill(dtItem);
                        comboBoxMake.ValueMember = "Item_Make";
                        comboBoxMake.DisplayMember = "Item_Make";
                        comboBoxMake.DataSource = dtItem;

                    }
                }
            }
           
       }


Please help me
Posted
Updated 3-Dec-14 18:24pm
v2
Comments
DamithSL 4-Dec-14 0:42am    
any exception?
have you debug and check whether you get correct data on name combobox selected item change event for make combobox?
Ashish Patil 4-Dec-14 1:06am    
1st combobox gets filled. but when i select item from 1st combobox, it does not populate 2nd combobox
DamithSL 4-Dec-14 1:21am    
put break point inside comboBoxName_SelectedIndexChanged and check the value you get as comboBoxName.Text. is it correct?
proceed with the debug and check the values and you may able to find the issue
Ashish Patil 4-Dec-14 2:16am    
i tried putting break point inside SelectedIndexChanged but it doesn't halt when index is changed.

1 solution

Did you check using break point.Did you get your selected item Name over here.

C#
cmd.Parameters.AddWithValue("@Item_Name",comboBoxName.Text);


try like this

C#
cmd.Parameters.AddWithValue("@Item_Name",comboBoxName.Selecteditem.Tostring());
 
Share this answer
 
Comments
Ashish Patil 4-Dec-14 1:21am    
I tried the above solution but it didn't work
syed shanu 4-Dec-14 1:23am    
did you check with break point.Did you get selected item text of your first combo box.
Did you check your Datatable using break point is the datas are populated to your datatable.
adp.Fill(dtItem);
comboBoxMake.ValueMember = "Item_Make";
syed shanu 4-Dec-14 1:24am    
and can you check your code.

comboBoxMake.ValueMember = "Item_Make";

comboBoxMake.DisplayMember = "Item_Make";

comboBoxMake.DataSource = dtItem;



change this to

comboBoxMake.DataSource = dtItem;

comboBoxMake.DisplayMember = "Item_Make";
comboBoxMake.ValueMember = "Item_Make";
Ashish Patil 4-Dec-14 3:30am    
thank you for the help buddy, i sorted it out by deleting the selectedIndexChanged event and writing the same in SelectedIndexChanged of Item_Name.
I dint know what went wrong before but its working now
syed shanu 4-Dec-14 3:32am    
OK Cool :)

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