pius
pius

Reputation: 31

Converting MySQL DATETIME to System.DateTime

I am using a MySQL database with DbLinq, but there is an exception being thrown:

"Cannot convert mysql datetime to system.datetime"

How can I resolve this issue?

Upvotes: 3

Views: 9731

Answers (4)

John
John

Reputation: 49

Check for any zero date means date like '0000-00-00' in database; this could be the cause of error.

Upvotes: 4

john plourd
john plourd

Reputation: 155

I was moving a column in my gridview from asp:BoundField to asp:TemplateField so had to display a date from a MySQL database explicitly. I found, through reflection, that the collection typed the date fields as MySqlDateTime and not system.DateTime.

I added the import MySql.Data.Types statement to the code behind and the following Eval statement within a label in the context of a gridview.

Text='<%# CType(Eval("Submitted_Date"), MySql.Data.Types.MySqlDateTime).ToString%>'

output format: 02/23/2011

Hope that helps!

Upvotes: 1

Donut
Donut

Reputation: 112795

Well, if you have a result set containing MySql DateTime values, you could use the ConvertAll method along with a delegate using DateTime.Parse to create a collection of System.DateTime values.

Here's an example, where "results" is the result of your dblinq query containing MySQL datetime values:

List<DateTime> dateTimes = results.ConvertAll(delegate(string time){ 
   return DateTime.Parse(time); 
});

Is this what you're looking for?

Upvotes: 1

Related Questions