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

10
  • what is req.FACorrectionIn1 is that the value from the database? Commented Mar 9, 2017 at 2:41
  • @un-lucky they are all null in database Commented Mar 9, 2017 at 2: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. Commented Mar 9, 2017 at 2:48
  • @KiRa what .Net type is req.FACorrectionIn1? are they string or date time Commented Mar 9, 2017 at 2:48
  • 2
    @KiRa, DateTime cannot be null unless changed to nullable type. ConvertTo.DateTime will return DateTime.MinValue if the value input is null. Commented Mar 9, 2017 at 2:57

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

What does DbNull means?
@KiRa: The keyword null represents an invalid reference. The class System.DbNull represents a nonexistent value in a database field, See this for more information regarding the difference
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: 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
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 ?
0

You should use HH:mm format.

Convert.ToDateTime(req.FAIn1).ToString("HH:mm");

Comments

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");

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.