-2

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

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
Mrkhan
  • 65
  • 7
  • 1
    What line of code throws the exception? – mjwills Aug 10 '18 at 11:44
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Aug 10 '18 at 11:44

1 Answers1

0

It seems to me that you're trying to pass the empty string "" where it should be an int.

Is your Cat_id set to autoincrement? If it is, just skip the Cat_id and pass only other values.

Alice
  • 173
  • 1
  • 15