I am working on a quite old application in which there were no parametrized query's that were used at that time.
I have to insert date time value in an column of sql
table with date-time
as data type, null value is not allowed in this column.
My code.
var expires = dtpExpires.Enabled ? dtpExpires.Value.ToString() : "'1/1/1900 12:00:00 AM'";
string query = "INSERT INTO route (expires) Values ("+ expires +")";
The problem with this is, When the date picker is disabled then a default value must be passed since null are not allowed. So for that I have to include an extra ''
to wrap around the date and it works correctly.
But when date picker is enabled and valid date time is trying to get inserted into database it fails due to lack of ''
this wrapped around the expires
variable.
Is there any clean approach to do this without parametrized query. the same problem will come while updating the code. Can there be clean approach for this to work on both the cases rather than adding just if-else
clause .