1

In my MySql database i have a date column, then in vb.net i am using this code:

If Not IsDate(reader3.GetDateTime(0).ToString("yyyy-MM-dd")) Then

End If

but if the date in the database is 0000-00-00 its returning an error:

Unable to convert MySQL date/time value to System.DateTime

i have also tried remove the ToString

If Not IsDate(reader3.GetDateTime(0)) Then

End If

but i still get the same error

charlie
  • 415
  • 4
  • 35
  • 83
  • 4
    see this http://stackoverflow.com/questions/23007765/unable-to-convert-mysql-date-time-value-to-system-datetime-couldnt-store-0-0-00 – Vivek S. Dec 11 '15 at 09:21
  • @wingedpanther The answer you refer to is wrong or out of date: In MySql `INSERT INTO myitems (MyDate) VALUES ('0000-00-00 00:00:00.000')` works correctly and a data/DateTime/Timestamp field can be NULL or '0000-00-00 00:00:00.000' – genespos Dec 11 '15 at 11:26

1 Answers1

1

A way is to use IF into your MySql Select:

IF(YourDateField='0000-00-00' OR YourDateField IS NULL,'',YourDateField) AS YourDateField

So, when the field is null or equal to '0000-00-00', the output from DB will be an empty string and you'll avoid vb.net errors.

genespos
  • 3,211
  • 6
  • 38
  • 70