How to load and save cateogry_id into a combobox from another table but in combobox it display Cateogry_Name c#?? My code for save:
con.Open();
comboBox1.Text.ToString();
String Query = "INSERT INTO tbl_Item (Cat_id, Barcode, ItemName, Unit, Quantity, SalePrice, PPrice) VALUES ('"+ comboBox1.ValueMember + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')";
SqlDataAdapter SDA = new SqlDataAdapter(Query, con);
SDA.SelectCommand.ExecuteNonQuery();
float mg = float.Parse(textBox6.Text) - float.Parse(textBox7.Text);
label10.Text = mg.ToString();
con.Close();
MessageBox.Show("Data Saved!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
And also a method
protected void FillCombo()
{
DataSet ds = new DataSet();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tbl_cateogry", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
comboBox1.DisplayMember = "Cat_Name";
comboBox1.ValueMember ="Cat_id";
comboBox1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
}
When i run this its show me "Conversion failed when converting the varchar value "cat_id" to data type int" Plz post the write code