且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#在windows窗体中级联组合框

更新时间:2023-12-06 11:15:46

您是否使用断点查看。您在此处获得所选项目名称。



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





试试这样



 cmd.Parameters.AddWithValue(  @ Item_Name,comboBoxName.Selecteditem.Tostring ()); 


I have a table named Item in sql server database. Item table definition is

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 :

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

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

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



try like this

cmd.Parameters.AddWithValue("@Item_Name",comboBoxName.Selecteditem.Tostring());