SQL.  vb.net,     Insert parameter gives String  to DateTime conversion error

Posted on 2012-09-08
Medium Priority
Last Modified: 2012-12-24
In vb.net I'm trying to Insert a record into my table that has 2 datetime columns.  The error raised is "Failed to convert parameter value from a String to a DateTime. "

The salient code for this error is:

 Dim UpdateTime As DateTime = DateTime.Parse(Now())   ' Parse to System datetime format

    ' ...set next two columns with Date/Time Created & Last Updated
    P = SqlCMD.Parameters.Add(New SqlParameter("@IconCreated", SqlDbType.DateTime))
    P.Value = UpdateTime
    P = SqlCMD.Parameters.Add(New SqlParameter("@IconLastUpdated", SqlDbType.DateTime))
    P.Value = UpdateTime

The error is raised when ExecuteNonQuery()  is executed.for the Insert

Thanks for any suggestions.
Question by:FrankBabz
  • 4
  • 2

Expert Comment

ID: 38380011
Could it be that the dateformat is wrong?

Maybe the dateformat is different on the SQL-server?


Author Comment

ID: 38380115
Thanks HTH,

Not sure where 'dateformat' is,  but....

I did check both columns in the table, and....  'Data Type' and 'System Type' are 'datetime'

LVL 55

Expert Comment

by:Jaime Olivares
ID: 38380302
Parse() is very risky, it depends on your current Culture settings. I suggest to try ParseExact
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Author Comment

ID: 38380952
My AP will have no inter-cultural change considerations.   USA is all I need.

Seems inconceivable they would be different, but I guess I should verify that DB and AP are defaulting to the same culture.    Where do I look for current culture settings for both?

LVL 55

Accepted Solution

Jaime Olivares earned 1500 total points
ID: 38381149
For most quality-code checking tools, it is always considered risky to assume default cultures. You should check it anyway. Some feasible cases:
Canadians can have different time formats and I guess some US-Citizens have Canadian computers.
Foreigners living in USA can have regional settings other than USA in their computers.

Author Comment

ID: 38381256
Jaimie....   I agree 100% with all you said.   However.......

In my case this AP is for use in our 1 and only office.  User's do not ever enter dates or times in this AP.    I have a learning curve with culture to implement something I (for now) do not need.   Maybe someday I will, but not now.

I need to get past the error message.  I feel pretty confident that DB and AP defaults for culture are the same.  But, I do not know how to prove that to you.

Author Comment

ID: 38391501
FYI - Problem fixed...

Dim UpdateTime As DateTime = DateTime.Parse(Now())   ' Parse to System datetime format

Should have been:
Dim UpdateTime As DateTime = Now()   '  To datetime format


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

621 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