Goma
Goma

Reputation: 53

Error (Failed to convert parameter value from a String to a DateTime)

I want to save Date and Time in Access DB field which its DataType is Date/Time. So this Error ( Failed to convert parameter value from a String to a DateTime )

Cmd.Parameters.Add(New OleDbParameter("@DateAdded", OleDbType.Date)).Value =
    Now.ToString("dd/MM/yyyy HH:mm:ss")
Cmd.Parameters.Add(New OleDbParameter("@DateModified", OleDbType.Date)).Value =
    Now.ToString("dd/MM/yyyy HH:mm:ss")

Upvotes: 0

Views: 363

Answers (1)

NineBerry
NineBerry

Reputation: 28499

Why try to convert to string? Just use the DateTime value directly.

Cmd.Parameters.Add(New OleDbParameter("@DateAdded", OleDbType.Date)).Value = Now
Cmd.Parameters.Add(New OleDbParameter("@DateModified", OleDbType.Date)).Value = Now

Your code most likely fails because the format you specify ("dd/MM/yyyy HH:mm:ss") is not the format of the culture your application runs with, so the systems fails converting the generated string to a DateTime because it does not recognize the format used.

Upvotes: 4

Related Questions