I always have problems with dates when I want to pass them as a parameter to a SQL statement, I try to use a previous variable, a Convert
or a Cast
but I can never find the correct way.
I have the following statement in C# where I am building an Update statement and I am sending 3 parameters, of which two are numeric values and the last one is the date in question.
UpdateStatement = "UPDATE Productos SET PrecioUnitario = " + nPrecioUnitario.ToString() + ", FactorVenta = " + nFactorVenta.ToString() + ", PrecioUnitarioFecha = CONVERT(DateTime,'" + dFecha.ToString() + "',103) WHERE ProductoId = " + nProductoId;
The error I get is the following:
Conversion failed when converting datetime from character string.
It is not a format issue because we are May 18 and the date is coming 05/18/2016 12:00:00. First I tried to leave it unconverted, then I put it in quotes and added Convert and I don't know at the end if there is any correct way to get a date to pass directly and without these problems.
Any ideas?
You should always use Parameters, like:
With more context/better example:
...Y:
You should not bind the values in the string, but assign the parameters to the command object
The structure should be something like
in this case both are understood
dFecha
andnFactorVenta
are of the typeDateTime