problem with time field in MySQL
-
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
-
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
-
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
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
-
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
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
-
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
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
-
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
What class type is returned from the
sql_reader["start_time"]
property? Check the documentation to see if it has aToString
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
-
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
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 aTimeSpan
to aDateTime
, which is why the call toConvert.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