0

I was trying to update the ProductQty column at a specific row in the ProductDB table but somehow no changes were made after execution.

For instance, at ID 1, the column of ProductQty is set to 30. I wanted to amend it to 29, but even after execution, the table remains unchanged.

OleDbConnection connect =
            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; 
                Data Source=POSDB.accdb; 
                Persist Security Info = False");
        connect.Open();
        string query = "UPDATE ProductDB SET [ProductQty] = @quandiff WHERE [ID] = @id";

        OleDbCommand command = new OleDbCommand(query, connect);
        command.Parameters.AddWithValue("@quandiff", quanDiff);
        command.Parameters.AddWithValue("@id", location);
        command.ExecuteNonQuery();

I tested the quanDiff and location variables beforehand via Console.Write and the data was retrieved alright. Just couldn't get the query working for whatever reason.

Unless I have done something wrong here, if you can point it out.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
benChung
  • 366
  • 4
  • 9
  • If other queries on the connection work, the problem may be `AddWithValue`. Try using an explicit type. – Gordon Linoff Nov 15 '15 at 15:27
  • What do you mean by _I wanted to amend it to 29_ exactly? You mean append or something? Do you wanna insert `quanDiff - location` instead of them? – Soner Gönül Nov 15 '15 at 15:31
  • What value is returned from ExecuteNonQuery? It is the number of rows affected. Is it zero? – Oguz Ozgul Nov 15 '15 at 15:45
  • Possible duplicate of http://stackoverflow.com/questions/1216271/whats-wrong-with-these-parameters. You cannot use named parameters. – trincot Nov 15 '15 at 16:06
  • Add the return value of _ExecuteNonQuery()_ it should be >0 if the row with the ID has been found and updated, zero if not. If you get a 1 but you don't see the changes then it is best to investigate if you are looking at the same database (in debug sessions under Visual Studio the working directory is the BIN\DEBUG one not the project folder) – Steve Nov 15 '15 at 16:24

0 Answers0