Reputation: 479
I am trying to write the current date into a database table. I have set the field of the table as 'date'. I am writing
DateTime.Now.Date.ToString
as a parameter to the insert/update query. So, my code is something like:
cmd.CommandText = "update tab set dt = @d where id=1"
cmd.Parameters.add("@d",DateTime.Now.Date.ToString)
This doesnt seem to be updating values in the database. Is something amiss here?
Upvotes: 1
Views: 1589
Reputation: 4151
If this is SQL Server get remove the parameter and change the SPROC to use GETDATE() that will cause this to always insert the SQL Server's current DateTime Stamp.
Upvotes: 1
Reputation: 498904
Don't pass in a string to something that expects a DATE
.
Pass in the DateTime
instance instead:
cmd.Parameters.AddWithValue("@d",DateTime.Now.Date)
Upvotes: 5