1

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?

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.