Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have 2 cascading combo-box in windows form application. I have textbox for price and unit. when I select first combobox, second combobox gets populated. I want textbox for price and unit to be filled only on second combobox selection. My problem is when the form is loaded both textboxes are filled with values from table and not on combobox selection changed.

my code is:
C#
private void Purchase_Load(object sender, EventArgs e)
    {

        // TODO: This line of code loads data into the 'supplierDataSet.Supplier' table. You can move, or remove it, as needed.
        this.supplierTableAdapter.Fill(this.supplierDataSet.Supplier);
        fillName();

        comboBoxName.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()
   {
       string str = "Select Item_Make from Item Where Item_Name=@Item_Name";
        using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
        {

            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.DataSource = dtItem;
                    comboBoxMake.ValueMember = "Item_Make";
                    comboBoxMake.DisplayMember = "Item_Make";


                }
            }
        }

   }

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

  private void comboBoxMake_SelectedIndexChanged_1(object sender, EventArgs e)
  {
      if (!string.IsNullOrEmpty(comboBoxMake.Text))
      {
          textBoxPrice.Enabled = true;
          textBoxUoM.Enabled = true;

      }

      SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
      SqlCommand cmd = new SqlCommand("Select * from Item Where Item_Make='" + comboBoxMake.Text + "' AND Item_Name='" + comboBoxName.Text + "'", con);
      SqlDataReader reader;
      try
      {
          if (con.State == ConnectionState.Closed)
          {
              con.Open();
          }
          reader = cmd.ExecuteReader();
          while (reader.Read())
          {
              textBoxPrice.Text = Convert.ToString(reader["Price"]);
              textBoxUoM.Text = Convert.ToString(reader["Unit"]);
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
      finally
      {
          if (con.State == ConnectionState.Open)
          {
              con.Close();
          }
      }
  }

I am stuck. please help
Posted
Updated 8-Dec-14 7:44am
v2

1 solution

This[^] may help you.
 
Share this answer
 
v4
Comments
BillWoodruff 8-Dec-14 12:08pm    
Warning: the link shown above leads to very strange browser behavior in Chrome: the page appears as one might expect and then, as you scroll down in it, without clicking, it goes to another page: http://ww1.csharpaspnetarticles.com/ which is completely "dark" with no content showing, and, after an interval another browser tab is opened with a form that wants to download a video player.

Whether this behavior is "normal" for a blogspot site, or a result of my using AdBlock, or whether a hi-jacking is going on here: I don't know. But, I do know I'd never download a video-player, or anything else, in a context like this.

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