Charlese Virility Charlese Virility - 1 month ago 17
C# Question

INSERT INTO WITH MYSQL

I'm having this error and I can't seem to fix it someone please help.

Here is my code of what I'm trying to do.

if (tb_additemid.Text != "" && tb_additemname.Text != "" && tb_additemprice.Text != "" && tb_additemstock.Text != "")
{
MySqlCommand save;
save = new MySqlCommand("INSERT INTO tblitems SET (ITEM_ID,ITEM_NAME,DATE_ACQUIRED,PRICE,STOCKS) VALUES (?,?,?,?,?)", global.connect);
save.Parameters.AddWithValue("@itemid", tb_additemid.Text);
save.Parameters.AddWithValue("@itemname", tb_additemname.Text);
save.Parameters.AddWithValue("@dateacquired", dtp_dateacquired.Value);
save.Parameters.AddWithValue("@price", int.Parse(tb_additemprice.Text));
save.Parameters.AddWithValue("@stocks", int.Parse(tb_additemstock.Text));
global.connect.Open();
save.ExecuteNonQuery();
MessageBox.Show("ITEM SAVED SUCCESSFULLY");
global.connect.Close();


And here's what I'm getting.

enter image description here

Answer

The Insert statement does not have set. The set is for update statement. So remove the set it should be like this:

save = new MySqlCommand("INSERT INTO tblitems (ITEM_ID,ITEM_NAME,DATE_ACQUIRED,PRICE,STOCKS) VALUES (@itemid,@itemname,@dateacquired,@price,@stocks)", global.connect);

Although it is possible to write the INSERT statement without specifying the column names only their values:

save = new MySqlCommand("INSERT INTO tblitems VALUES (@itemid,@itemname,@dateacquired,@price,@stocks)", global.connect);