Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want if selected items in the combo box where the value of combo box item is queried by database, the result of selected item in combo box will show in text box

What I have tried:

/* code get data from access db into combo box */
C#
private void GetProductsName()
      {
          try
          {
              OleDbConnection conn = new OleDbConnection();
              conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\xxx\Documents\RetailDatabse.accdb;Persist Security Info=False";
              conn.Open();

              OleDbCommand cmd = new OleDbCommand();
              cmd.Connection = conn;
              cmd.CommandText = "SELECT ProductName, ProductPrice FROM Products";
              OleDbDataReader reader = cmd.ExecuteReader();
              while (reader.Read())
              {
                  ItemNamecomboBox.Items.Add(reader["ProductPrice"].ToString() + "  " + reader["ProductName"].ToString());
                  //ItemNamecomboBox.Items.Add(reader["ProductPrice"].ToString());

              }

              conn.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error query" + ex);
          }
      }


/* showing data from selected combo box that get error */
C#
private void ItemNamecomboBox_SelectedValueChanged(object sender, EventArgs e)
       {

           decimal productPrice = Convert.ToDecimal(ItemNamecomboBox.SelectedValue);
           UnitPricetextBox.Text = productPrice.ToString();

       }
Posted
Updated 7-Oct-16 16:55pm

1 solution

try this

C#
ItemNamecomboBox.DisplayMember = "Text";
            ItemNamecomboBox.ValueMember = "Value";
            while (reader.Read())
            {
                ItemNamecomboBox.Items.Add(new { Text = reader["ProductPrice"].ToString() + "  " + reader["ProductName"].ToString(), Value = reader["ProductPrice"].ToString() });
            } 


C#
private void ItemNamecomboBox_SelectedValueChanged(object sender, EventArgs e)
       {
           dynamic item = ItemNamecomboBox.SelectedItem;
           if (item != null)
           {
               string price = item.Value;
           }
       }
 
Share this answer
 
Comments
Member 10195910 8-Oct-16 18:26pm    
Hi, your solution is still gets any error for below code
private void ItemNamecomboBox_SelectedValueChanged(object sender, EventArgs e)
{
dynamic item = ItemNamecomboBox.SelectedItem;

if (item != null)
{
string price = item.Value; // the one get error
UnitPricetextBox.Text = price;

}
}

Error Output :
RuntimeBindeException was unhandled
<>fAnonymousType0
Karthik_Mahalingam 9-Oct-16 0:36am    
make sure these 2 lines are declared before the loop
ItemNamecomboBox.DisplayMember = "Text";
ItemNamecomboBox.ValueMember = "Value";

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