Anirudha Gupta
Anirudha Gupta

Reputation: 9279

wrong conversion done in c# for Mysql?

i have the code in c#

   string date = reader["date"].ToString();

and got error that

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

when i convert them to string then why it's trying to convert them to datetime. well the value they create error is 0000-00-00 00:00:00

Upvotes: 2

Views: 1877

Answers (3)

ChrisBD
ChrisBD

Reputation: 9209

That's because the database call is returning a MySqlDateTime class object. Try the following code:

MySqlDateTime mysqlDate = reader["date"];
DateTime date = mysqlDate.GetDateTime();
string date = date.ToString();

Upvotes: 0

Stecya
Stecya

Reputation: 23266

Because 0000-00-00 00:00:00 is not valid .Net DateTime (minimum value is 00:00:00.0000000, January 1, 0001)

Add this to the connection string :

Allow Zero Datetime=true and Convert Zero Datetime=true

Upvotes: 2

Then you should do some changes like follows in your connection string

Allow Zero Datetime=true 

To know why you should do like that see this conversation

Upvotes: 0

Related Questions