Declaración de actualización de casilla de verificación

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.

preguntado el 24 de agosto de 12 a las 05:08

I would expect to set to 1, No a true. -

Oh, and be aware of inyección SQL. -

What problem do you have or what exception raised? -

@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. -

2 Respuestas

Prueba esto: -

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.

Respondido 24 ago 12, 05:08

and also using the "1" instead of "true". - jpl

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.

Prueba esto:

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

Respondido 24 ago 12, 05:08

I feel so dumb now... Thank you very much. Your help is appreciated. - jpl

Its ok, happens sometimes. :-) - nunespascal

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.