Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2516
  • Last Modified:

Error converting data type nvarchar to datetime?

I'm getting the error "Error converting data type nvarchar to datetime" on the following line:

            cmd.Parameters.AddWithValue("@startTime", row("startTime")).Value = If(row("startTime").ToString.Length = 0, DBNull.Value, row("startTime"))

Open in new window


In the Database table this column ("startTime") is defined as a DateTime data type. Is there a better way to code this line?

Thanks!
0
BlakeMcKenna
Asked:
BlakeMcKenna
1 Solution
 
Ron MalmsteadInformation Services ManagerCommented:
That line looks fine..

However, in your update or insert statement.. try the following...

StartTime=Convert(datetime,@startTime)
0
 
Jacques Bourgeois (James Burger)Commented:
Look at the way you DataTable is built and the type of row("startTime")).Value in the debugger. If the "StartTime" field is a datetime field from the start, then something is wrong if row("startTime")).Value is a String, which is what the error message seems to imply.

The problem could also come from the fact that you are sending row("startTime") as the value of the parameter. This is an Object, the system converts it as it sees fit, and it might decide to convert it to a String. You might be better of forcing a date:

CDate(row("startTime")))
0
 
BlakeMcKennaAuthor Commented:
James,

I tried your solution and that worked!

Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now