Pull the datetime value down as a string and do a DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyyy", culture, styles); You would just need to set the date format up for the date you are returning from the database. Most likely it's yyyy-MM-dd HH:mm:ss. At least is is for me.
If I google for "Unable to convert MySQL date/time value to System.DateTime" I see numerous references to a problem accessing MySQL from Visual Studio. Is that your context?
One solution suggested is:
This is not a bug but expected
behavior. Please check manual under
connect options and set "Allow Zero
Datetime" to true, as on attached
pictures, and the error will go away.
I also faced the same problem, and get the columns name and its types. Then cast(col_Name as Char) from table name. From this way i get the problem as '0000-00-00 00:00:00' then I Update as valid date and time the error gets away for my case.
You can make the application fully compatible with the date and time that is used by MySql. When the application runs at runtime provide this code.
First Go to the Application Events.
In the list of tools
Go to the project
Project Properties
Select the Application tab
View Application Events
This will open a new file. This file contains code used at the start of the application.
Write this code in that new file:
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Application.ChangeCulture("en")
My.Application.ChangeUICulture("en")
My.Application.Culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
My.Application.Culture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd"
My.Application.Culture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
My.Application.Culture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
End Sub
End Class
Rather than changing the connection string, you can use the IsValidDateTime property of the MySqlDateTime object to help you determine if you can cast the object as a DateTime.
I had a scenario where I was trying to load data from an "UpdateTime" column that was only explicitly set when there was an update to the row (as opposed to the InsertedTime which was always set). For this case, I used the MySqlDataReader.GetMySqlDateTime method like so: