'Get DateTime from Mysql

I am saving date and times in Mysql (in the save column) and now I am trying to make a plot using date and time and temperature. I'm using:

DateTime tm = myReader.GetDateTime("dateandtime");

(dateandtime is saved to the database and contains, for example, 15.04.2015 10:55:35).

My problem is when I try to make the graphic this error comes up:

"input string was not in a correct format"

And I don't know how to fix it. I tried to only write the date (ex 15.04.2015) to the database and it works, but I want the complete date and time to be saved.

Any ideas to solve this problem?

        string constring = "datasource=localhost;port=3306;username=root;password=";           
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand("select * from test.edata;", conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                DateTime tm = myReader.GetDateTime("dataandtime");                    
                double tempbd = myReader.GetDouble("temperatura");
                //DateTime tr = Convert.ToDateTime(tm);
                listbd.Add(tm.ToOADate(), tempbd);
            }
            zedGraphControl2.AxisChange();
            zedGraphControl2.Refresh();
            zedGraphControl2.Invalidate();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

I don't understand why on my X axis i have only Minute:Second :(.I add a breakpoint and the "tm" variable show the complete date and time ,but on my plot i have only minute and second



Solution 1:[1]

First of all, GetDateTime method takes zero based column number as an int. That's why, your code does not even compile. You will need to use it as;

DateTime tm = myReader.GetDateTime(3);

Second of all, GetDateTime does not performing any conversation, that's why your column value must be already a DateTime object.

I strongly suspect you try to save your DateTime values as a character. Please don't do that. Use proper column types always.

Read: Bad habits to kick : choosing the wrong data type

You need to change your column type to DATETIME first, insert your DateTime values directly to your parameterized queries and use GetDateTime method with the number of it's ordinal when you try to get them in your database.

Also use using statement to dispose your connections, commands and readers.

Solution 2:[2]

Maybe you can use DateTime.Now;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Jules