I have a C# application with a datagrid in windows form. I need to update my qty from 0 to 500 and update to my database. however I got messagebox that says "
you have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '50 where pcode like 'ph003'" at line 1
public void LoadStockIn()
{
int i = 0;
dataGridView2.Rows.Clear();
con.conDB.Open();
cmd = new MySqlCommand("SELECT * FROM vwstockin where refno like '" + txtrefno.Text + "' and status like 'Pending'", con.conDB);
dr = cmd.ExecuteReader();
while (dr.Read())
{
i++;
dataGridView2.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString());
}
dr.Close();
con.conDB.Close();
}
private void savebtn_Click(object sender, EventArgs e)
{
try
{
if(dataGridView2.Rows.Count > 0 )
{
if (MessageBox.Show("Are you sure you want to save this records?", stitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
{
for (int i =0; i< dataGridView2.Rows.Count; i++)
{
//Update product qty
con.conDB.Open();
cmd = new MySqlCommand("Update tblproduct set qty = qty " + int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString()) +" WHERE pcode like '" +dataGridView2.Rows[i].Cells[3].Value.ToString() + "'",con.conDB);
cmd.ExecuteNonQuery();
con.conDB.Close();
//updatet tblstock qty
con.conDB.Open();
cmd = new MySqlCommand("update tblstock set qty = qty + " + int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString()) + ", status = 'Done' where id like '" + dataGridView2.Rows[i].Cells[1].Value.ToString() + "'",con.conDB);
cmd.ExecuteNonQuery();
con.conDB.Close();
}
Clear();
LoadStockIn();
}
}
}
catch(Exception ex)
{
con.conDB.Close();
MessageBox.Show(ex.Message, stitle);
}
}
This is culprit qty = qty " + int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString()) +" . It should be qty = int.Parse(dataGridView2.Rows[i].Cells[5].Value.ToString()) +"
.
Basically you have int datatype for this column. and you are providing wrong value.
Make set qty = " +
and it will work.