1

I would like to know how to update a checkbox using an update statement in the code behind.

I am using a gridview with order information with a checkbox to set if the order is completed or not. Initially it is set to false.

I have a dropdownlist with the order id number that I am using in my update statement (the variable I use is OrderID)

I tried this update statement but it seems like it is not working:

strSQL = "UPDATE Order SET OrderCompleted = true  WHERE OrderID = " + OrderID;

Can anyone help, I have been researching this for awhile now.

4
  • 1
    I would expect to set to 1, not to true.
    – Uwe Keim
    Aug 24, 2012 at 4:41
  • 1
    Oh, and be aware of SQL injection.
    – Uwe Keim
    Aug 24, 2012 at 4:42
  • 1
    What problem do you have or what exception raised?
    – SMK
    Aug 24, 2012 at 4:42
  • 1
    @Uwe Kein, I am using this to prevent sql injection: cmd.Parameters.AddWithValue("OrderID", OrderID); and Shoaib, there are not exception raised, it was just not working.
    – jpl
    Aug 24, 2012 at 5:19

2 Answers 2

3

Try this: -

strSQL = "UPDATE [Order] SET OrderCompleted = 1 WHERE OrderID = " + OrderID;

In SQL there is no data type for boolean but there is a data type bit which accepts zero (0) or one (1) but not true or false. As you are using a checkbox so you need to convert boolean value into integer before passing into the query.

Secondly, as mentioned by @nunespascal braces [] around Order word because Order is an SQL keyword.

1
  • and also using the "1" instead of "true".
    – jpl
    Aug 24, 2012 at 5:21
2

Order is a reserved word in sql. Used with the order by clause to sort rows.

Encapsulate your table and column names with [] if they are keywords.

Try this:

strSQL = "UPDATE [Order] SET OrderCompleted = 1 WHERE OrderID = " + OrderID + "";
2
  • I feel so dumb now... Thank you very much. Your help is appreciated.
    – jpl
    Aug 24, 2012 at 4:43
  • Its ok, happens sometimes. :-) Aug 24, 2012 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.