I'm using Visual Studio c# (spanish)

The code:

cmd.CommandText = "UPDATE cursos SET saldo = saldo - '" + cantidad.ToString() + "' where id = '" + vID + "' ";

vID is a string variable.

The problem is here:

Don't work where id = '" + vID + "' neither where id = '" + vID.ToString() + "'

The error:
(Translating) Don't match data type in criteria expression

saldo working OK.

Can you help me?. Thx.

Edited 5 Years Ago by jotae: n/a

You are trying to do math with a string saldo = saldo - '" + cantidad.ToString() + "' . If cantidad is a number, then you want saldo = saldo - " + cantidad ... . Leave out the single quotes (') around cantidad.

Edited 5 Years Ago by Momerath: n/a

Thx but the problem is not in SALDO. This work fine. The problem is in WHERE ID =

Use parameterized query to avoid datatype mismatch.

cmd.CommandText = "UPDATE cursos SET saldo = saldo - @cantidad where [id] = @id";
cmd.Parameters.AddWithValue("@cantidad",cantidad);
cmd.Parameters.AddWithValue("@id",vID);

thx adatapost!! Solved with parameters. Working fine.

Edited 5 Years Ago by jotae: n/a

You're welcome!

I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question and good luck!

This question has already been answered. Start a new discussion instead.