none
problem with time field in MySQL RRS feed

  • Question

  • Hi,

    I have WinForm application.

    I am trying to display a value from MySQL. The field type is time and it's NOT null.

    I am using this code:

    lblStartTime.Text = Convert.ToDateTime(sql_reader["start_time"]).ToString("HH:mm");

    but getting this error message:

    Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.

    Please Help...

    Friday, September 13, 2013 10:24 PM

Answers

  • I tried this:

    lblStartTime.Text = sql_reader["start_time"].ToString("HH:mm");

    but got:

    Error 28 No overload for method 'ToString' takes 1 arguments



    Then check this:

    lblStartTime.Text = ((TimeSpan)sql_reader["start_time"]).ToString("hh':'mm");


    Sunday, September 15, 2013 8:11 AM

All replies

  • The field in the database is DateTime so you don't need to convert to a DateTime.

    lblStartTime.Text = sql_reader["start_time"].ToString("HH:mm");


    jdweng

    Saturday, September 14, 2013 8:10 AM
  • [...]
    Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.


    According to error message, the database returns TimeSpan. It cannot be converted to DateTime, but you can represent it as a string using this statement:

    lblStartTime.Text = sql_reader["start_time"].ToString("hh':'mm");


    Saturday, September 14, 2013 5:38 PM
  • I tried this:

    lblStartTime.Text = sql_reader["start_time"].ToString("HH:mm");

    but got:

    Error 28 No overload for method 'ToString' takes 1 arguments
    Saturday, September 14, 2013 7:58 PM
  • this is my database structure for your info:

    CREATE TABLE `job_orders` ( `job_order_id` int(11) NOT NULL AUTO_INCREMENT, `customer_id` int(11) DEFAULT NULL, `cleaner_id` int(11) DEFAULT NULL, `job_order_status` int(255) DEFAULT '1' COMMENT 'New, Cancelled, Completed, Payment', `is_cancelled` bit(1) DEFAULT b'0', `job_order_date` date DEFAULT NULL, `start_time` time DEFAULT NULL, `end_time` time DEFAULT NULL, `job_order_note` text, `created_date` datetime DEFAULT NULL, `created_user` int(11) DEFAULT NULL, PRIMARY KEY (`job_order_id`), UNIQUE KEY `idx_job_orders_job_order_id` (`job_order_id`), KEY `idx_job_orders_customer_id` (`customer_id`), KEY `idx_job_orders_cleaner_id` (`cleaner_id`), KEY `idx_job_orders_job_order_status` (`job_order_status`), KEY `idx_job_orders_is_cancelled` (`is_cancelled`), KEY `idx_job_orders_job_order_date` (`job_order_date`), KEY `idx_job_orders_start_time` (`start_time`), KEY `idx_job_orders_end_time` (`end_time`), KEY `idx_job_orders_created_date` (`created_date`), KEY `idx_job_orders_created_user` (`created_user`) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

    • Edited by Jassim Rahma Saturday, September 14, 2013 8:29 PM
    Saturday, September 14, 2013 8:29 PM
  • You have to cast the field return by the DataBase to a DateTime object.

                lblStartTime.Text = ((DateTime)sql_reader["start_time"]).ToString("HH:mm");


    jdweng

    Saturday, September 14, 2013 8:55 PM
  • You have to cast the field return by the DataBase to a DateTime object.

                lblStartTime.Text = ((DateTime)sql_reader["start_time"]).ToString("HH:mm");


    jdweng

    Getting:

    Specified cast is not valid.

    Saturday, September 14, 2013 9:13 PM
  • You may be getting a null back from the database.  Alos a "time" object in MySQL doesn't contain a date.

                if (sql_reader["start_time"] != null)
                    lblStartTime.Text = ((Time)sql_reader["start_time"]).ToString("HH:mm");
                else
                    lblStartTime.Text = "null";


    • Edited by Joel Engineer Saturday, September 14, 2013 9:44 PM error
    Saturday, September 14, 2013 9:38 PM
  • I am getting the same error on this line:

    lblStartTime.Text = ((DateTime)sql_reader["start_time"]).ToString("HH:mm");

    The value in the table is 03:00:00

    Saturday, September 14, 2013 9:43 PM
  • I tried this:

    lblStartTime.Text = sql_reader["start_time"].ToString("HH:mm");

    but got:

    Error 28 No overload for method 'ToString' takes 1 arguments



    Then check this:

    lblStartTime.Text = ((TimeSpan)sql_reader["start_time"]).ToString("hh':'mm");


    Sunday, September 15, 2013 8:11 AM