Solved

Error converting data type nvarchar to datetime?

Posted on 2014-07-28
3
2,181 Views
Last Modified: 2014-07-29
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
Comment
Question by:BlakeMcKenna
3 Comments
 
LVL 25

Expert Comment

by:Ron M
ID: 40225804
That line looks fine..

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

StartTime=Convert(datetime,@startTime)
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40225904
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
 

Author Closing Comment

by:BlakeMcKenna
ID: 40226622
James,

I tried your solution and that worked!

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expando 4 47
Showdialog 8 29
Populating an array of locale_time_info 8 37
Advice in Xamarin 21 56
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now