0

I'm trying to update a database using C#, but I get the error: "Syntax error in UPDATE statement". I've looked around for other examples of this error, and found plenty, but every one is different. A lot of things can cause this error, and I just can't put my finger on this one.

query = String.Format(@"UPDATE PAYMENT 
                    SET MONTANT={0}, TYPE='4-Comptant',note='PPA',flag='O', date='{2:yyyyMMdd}'       
                    WHERE num_payment={1}", -payment, id, dt);
                    daUpdate.UpdateCommand.CommandText = query;
                    daUpdate.UpdateCommand.ExecuteNonQuery();  //update the table in the database

In debug mode, this is what the string ends up looking like:

UPDATE PAYMENT 
                                                SET MONTANT=-390, TYPE='4-Comptant',note='PPA',flag='O', date='20120601'
                                                WHERE num_payment=8

In the database, num_payment is a long integer and a primary key. MONTANT is a double, everything else is text.

Edit: Following people's advice, I've modified things a bit, but I'm still getting the same error. Here's what it looks like now:

                    OleDbCommand cmd = _con.CreateCommand();
                    cmd.CommandText = @"UPDATE PAYMENT 
                    SET MONTANT=@montant, [TYPE]='4-Comptant',note='PPA',flag='O', [date]=@theDate 
                    WHERE num_payment=@numPayment";

                    cmd.Parameters.AddWithValue("@montant", -payment);
                    cmd.Parameters.AddWithValue("@theDate", String.Format("{0:yyyyMMdd}", dt));
                    cmd.Parameters.AddWithValue("@numPayment", id);
                    cmd.ExecuteNonQuery();

The debug output looks the same as before, except that now I see '@something' instead of the real value.

14
  • Try a different date format: date='{2:yyyy-MM-dd}' Commented Jun 1, 2012 at 16:40
  • database, num_payment is a long integer and a primary key. MONTANT is a double, everything else is text Something's not what you think it is. See here Commented Jun 1, 2012 at 16:49
  • 2
    Use parameterized SQL (for multiple reasons). Commented Jun 1, 2012 at 16:57
  • @agent-j: I can't, the database is the way it is, I'm not allowed to change the formats. @ Commented Jun 1, 2012 at 18:46
  • @agent-j: I'm not sure what you mean by strong, it shows up as "text" is MS Access. What I meant was that my boss gave me this database as it is, with entries already in this format. He doesn't want me changing the formats. Commented Jun 1, 2012 at 19:35

2 Answers 2

1

Put square brackets around [TYPE] and [DATE] these are SQL keywords.

query = String.Format(@"UPDATE PAYMENT  
                SET MONTANT={0}, [TYPE]='4-Comptant',note='PPA',flag='O', [date]='{2:yyyyMMdd}'        
                WHERE num_payment={1}", -payment, id, dt); 
                daUpdate.UpdateCommand.CommandText = query; 
                daUpdate.UpdateCommand.ExecuteNonQuery();  //update the table in the 
Sign up to request clarification or add additional context in comments.

2 Comments

The question is tagged mysql, and MySQL doesn't use [] as name delimiters. TYPE and DATE are not reserved words in MySQL (DATE is a keyword, but not a reserved word).
Actually, that's my mistake, its just plain SQL. I've removed the tag.
1

The fields 'note' and 'flag' needed to have [] around them. I added these and now the query doesn't give that error anymore.

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.