OleDbCommand cmd = new OleDbCommand("SELECT Stock FROM Products WHERE ID=" + ProductID + ";", conn); //run the database query
OleDbDataReader cusReader = cmd.ExecuteReader(); //read the result of the query
cusReader.Read();
ProductStock = (int)cusReader.GetValue(0);
cusReader.Close();
MessageBox.Show((ProductStock).ToString()); // checks that the form is being accessed and the SELECT query works
OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + "WHERE ID= " + ProductID +";", conn);
try
{
if (cusReader.RecordsAffected > 0)
{
MessageBox.Show("no issue was experienced");
}
else
{
MessageBox.Show("An issue occured when decreasing stock");
}
cusReader.Close();
}
catch (Exception ex)
{
MessageBox.Show("An error occured in query.\n" + ex.Message);
}
The update query returns the "an issue occurred" message. The message box that shows the variable Productstock does return the correct value. Can anbody explain how to fix this issue?
ExecuteNonQueryoncmd1. I guess that's what @Dirk meant.ExecuteNonQuery