-1

I'm facing a problem with decrementing value with MS Access database.

I get an error

Syntax error in UPDATE Statement

My code:

connection.Open();
command = new OleDbCommand();
command.Connection = connection;
command.CommandText = " update Cards set Count = Count - 1 where Type=" + ct + " ";
command.ExecuteNonQuery();
connection.Close();

Can anyone please help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omar
  • 17
  • 6

2 Answers2

1

You should provide an actual error. My guess is that count is a keyword and has to be put in square brackets like so [count] and do use parameters, see Joel's answer

Daniel N
  • 1,122
  • 1
  • 8
  • 14
0

It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:

using (var connection = new OleDbConnection("connection string here"))
using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))
{
    //have to guess at the OleDbType value. Use the actual column type and length from the database
    cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
    connection.Open();
    command.ExecuteNonQuery();
}

There are several other important fixes in this pattern, too.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794