0

As my title above I got a table in my database that is DateTime datatype and it is null value. And I got this line of code that I am using but I don't know why that it is returning 12:00

string FitaCorIn1 = Convert.ToDateTime(req.FACorrectionIn1).ToString("hh:mm") == null ? "00:00" : Convert.ToDateTime(req.FAIn1).ToString("hh:mm");
string FitaCorOut1 = Convert.ToDateTime(req.FACorrectionOut1).ToString("hh:mm") == null ? "00:00" : Convert.ToDateTime(req.FAIn1).ToString("hh:mm");

So as you can see in my code If the value of datetime is null I want to display 00:00 and if it is not null it will display the current value.

NOTE

12 Hours Format

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
KiRa
  • 924
  • 3
  • 17
  • 42
  • what is `req.FACorrectionIn1` is that the value from the database? – sujith karivelil Mar 09 '17 at 02:41
  • @un-lucky they are all null in database – KiRa Mar 09 '17 at 02:42
  • 1
    It seems like it's returning "12:00" because the `ToString` method is returning "12:00". Comparing that to null is not evaluating to TRUE. Maybe you don't want to compare the return from `ToString` to null. And maybe you don't even need to call the `ToDateTime` method. Maybe you are wanting to just check if the `FACorrectionIn1` member is null. – spencer7593 Mar 09 '17 at 02:48
  • @KiRa what .Net type is `req.FACorrectionIn1`? are they string or date time – Nkosi Mar 09 '17 at 02:48
  • @spencer7593 yes I want to check if it is null first – KiRa Mar 09 '17 at 02:54
  • 2
    @KiRa, `DateTime` cannot be null unless changed to nullable type. `ConvertTo.DateTime` will return `DateTime.MinValue` if the `value` input is `null`. – Nkosi Mar 09 '17 at 02:57
  • @KiRa also if you want 12hr format why return `00:00` if value is `null`? – Nkosi Mar 09 '17 at 03:01
  • @Nkosi there is a reason to it.. if the value of datetime in dbase is null I want to display in my jqgrid is `00:00` – KiRa Mar 09 '17 at 03:04
  • There is no 0:00 in 12 hour format. 12 hour format is 1:00 through 12:59 (Yes, this looks weird because 12:00 is earlier than 1:00 despite being a higher number). 24 hour is 0:00 through 23:59. – Powerlord Mar 09 '17 at 03:33
  • @Powerlord what I mean in that `00:00` is there is nothing edited time or null value in database. That's my reason. – KiRa Mar 09 '17 at 03:40

3 Answers3

1

You should check for null before converting them, otherwise you may get Exceptions from convert method. (As per the comments) It seems the FACorrectionIn1 is of type DateTime if so you have to check compare them with DateTime.MinValue if it is a nullable DateTime you can check for null as well.

Why you are getting 12:00 even when the value in the database is null

Same reason FACorrectionIn1 is a DateTime object, and so it won't be null so the condition that check for null became false since it's default value is 01/01/0001 00:00:00. So when you format them using .ToString("hh:mm") you will get 12:00. So you have to do like this:

string FitaCorIn1 = req.FACorrectionIn1 == DateTime.MinValue ? "00:00" : 
                                           Convert.ToDateTime(req.FAIn1).ToString("hh:mm");

It would be great if you use parsing instead for Convert.To..

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • What does `DbNull` means? – KiRa Mar 09 '17 at 02:54
  • @KiRa: The keyword null represents an invalid reference. The class System.DbNull represents a nonexistent value in a database field, See [this](http://stackoverflow.com/questions/4958379/what-is-the-difference-between-null-and-system-dbnull-value) for more information regarding the difference – sujith karivelil Mar 09 '17 at 02:56
  • If I use your method I got an error. Operator '==' cannot be apply to operands of type 'System.DateTime' and System.DBNull.. And If I use single '=' I also got an error Cannot Implicity convert type 'System.DBNull' to bool. – KiRa Mar 09 '17 at 03:04
  • @KiRa: Which means it is a DateTime object, so you have to use `null`(if the field is nullable type) DateTime.MinValue for comparison. see my updates – sujith karivelil Mar 09 '17 at 03:11
  • Looks like the column in db is nullable but the property of the class in not null. You need to change type of property to DateTime?. Also are you sure that the column in db table is nullable ? – Chetan Mar 09 '17 at 03:14
0

You should use HH:mm format.

Convert.ToDateTime(req.FAIn1).ToString("HH:mm");
Rai Vu
  • 1,595
  • 1
  • 20
  • 30
0

I got it now.. Thanks to spencer7593.. This is my code looks like now.

 string FitaCorIn1 = req.FACorrectionIn1 == null ? "00:00" : Convert.ToDateTime(req.FACorrectionIn1).ToString("hh:mm");
 string FitaCorOut1 = req.FACorrectionOut1 == null ? "00:00" : Convert.ToDateTime(req.FACorrectionOut1).ToString("hh:mm");
KiRa
  • 924
  • 3
  • 17
  • 42
  • 1
    Those properties seem to already be date times so there is no need to convert them. also DateTime cannot be null so condition statements will always go to the else statement. – Nkosi Mar 09 '17 at 03:08