2
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?

5
  • 3
    Are you even executing the update-statement? Commented Oct 5, 2016 at 9:27
  • 1
    You may want to call ExecuteNonQuery on cmd1. I guess that's what @Dirk meant. Commented Oct 5, 2016 at 9:32
  • the code that isn't working was copied word for word from an update query that worked perfectly, so I'm assuming I have Commented Oct 5, 2016 at 9:35
  • @peter.petrov yes, and of course also put a space in the query like you said in your answer. Commented Oct 5, 2016 at 9:36
  • I'm not exactly sure how to use ExecuteNonQuery Commented Oct 5, 2016 at 9:43

4 Answers 4

2

Put a space here right before WHERE in + "WHERE ID= " +

Also, make sure you call ExecuteNonQuery on cmd1.

Sign up to request clarification or add additional context in comments.

Comments

0

Please try this.It should work

OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + " WHERE ID= " + ProductID, conn);

Comments

0
OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + " WHERE ID= " + ProductID +";", conn);

Please use above command.

Problem: No space between stock value and where

Comments

0

Try update in one go (do not separate select and update: someone can modify the data after you've performed select but before you've started update), and then check rows affected. Make you sql being readable to avoid evident errors.

string sql = 
  // Parametrize your query or at least format it out: 
  $@"update Products
        set Stock = Stock - 1
      where Id = {ProductId} and
            Stock >= 1 -- added: you must have stock to decrease";

// Wrap IDisposable into using
using (OleDbCommand cmd = new OleDbCommand(sql, conn)) {
  int affected = cmd.ExecuteNonQuery();

  if (affected > 0) 
    MessageBox.Show("No issue was experienced");
  else
    MessageBox.Show("An issue occured when decreasing stock");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.