Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem with time field in MySQL

problem with time field in MySQL

Scheduled Pinned Locked Moved C#
helpdatabasemysqlcomannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    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..

    Technology News @ www.JassimRahma.com

    M L Richard DeemingR 3 Replies Last reply
    0
    • J Jassim Rahma

      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..

      Technology News @ www.JassimRahma.com

      M Offline
      M Offline
      Midnight Ahri
      wrote on last edited by
      #2

      Maybe this[^] could help you.

      1 Reply Last reply
      0
      • J Jassim Rahma

        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..

        Technology News @ www.JassimRahma.com

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

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

        OK, let's break this down into its constituent parts: sql_reader["start_time"] returns a Time value. .ToString("HH:mm"); converts that to a string in the form HH:mm. Convert.ToDateTime converts that string into a DateTime value. lblStartTime.Text = which you than try to assign to a Text field.

        Veni, vidi, abiit domum

        J 1 Reply Last reply
        0
        • L Lost User

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

          OK, let's break this down into its constituent parts: sql_reader["start_time"] returns a Time value. .ToString("HH:mm"); converts that to a string in the form HH:mm. Convert.ToDateTime converts that string into a DateTime value. lblStartTime.Text = which you than try to assign to a Text field.

          Veni, vidi, abiit domum

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          I tried this:

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

          but got: Error 28 No overload for method 'ToString' takes 1 arguments

          Technology News @ www.JassimRahma.com

          J L 2 Replies Last reply
          0
          • J Jassim Rahma

            I tried this:

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

            but got: Error 28 No overload for method 'ToString' takes 1 arguments

            Technology News @ www.JassimRahma.com

            J Offline
            J Offline
            Jassim Rahma
            wrote on last edited by
            #5

            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;

            Technology News @ www.JassimRahma.com

            1 Reply Last reply
            0
            • J Jassim Rahma

              I tried this:

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

              but got: Error 28 No overload for method 'ToString' takes 1 arguments

              Technology News @ www.JassimRahma.com

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              What class type is returned from the sql_reader["start_time"] property? Check the documentation to see if it has a ToString method, and if so what is the syntax? You cannot write code like this and just assume it will work, you need to ensure you are using valid properties and methods.

              Veni, vidi, abiit domum

              1 Reply Last reply
              0
              • J Jassim Rahma

                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..

                Technology News @ www.JassimRahma.com

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Jassim Rahma wrote:

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

                sql_reader["start_time"] is returning a TimeSpan[^]. You cannot convert a TimeSpan to a DateTime, which is why the call to Convert.ToDateTime is failing. If your time value is always less than 24 hours, you could use:

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

                However, note that the hh format specifier only includes "the number of whole hours in the time interval that is not counted as part of its day component": http://msdn.microsoft.com/en-us/library/ee372287.aspx[^] If your time values could exceed 24 hours, you would need to format the value yourself:

                private static string FormatTime(TimeSpan time)
                {
                return string.Format("{0:##00}.{1:00}", time.TotalHours, time.Minutes);
                }

                lblStartTime.Text = FormatTime((TimeSpan)sql_reader["start_time"]);


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups