0

I define this in my class

 public DateTime? LineCheckSubmitDateTime { set; get; }

I need to initialize this variable using my gridview

enter image description here

But sometimes i need to leave this value to be null ,But when i leave it it returns 1 / 1 / 0001 12:00:00 AM

so here is my code :

 newObj.LineCheckSubmitDateTime = (Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime"))==DateTime.Parse("1 / 1 / 0001 12:00:00 AM") ) ?   Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime")):null;

So two questions:

1:this code returns this error :

Severity    Code    Description Project File    Line
Error   CS0173  Type of conditional expression cannot be determined because there is no implicit conversion between 'DateTime' and '<null>' 

2:do you have better solution?

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

1 Answers1

2

The error is because you have to explicitly cast at least one of the operand of conditional operator to DateTime?

A better way to do it would be to compare it with DateTime.MinValue instead of converting minimum date string to DateTime, and also cache the converted value and then use it in the conditional operator instead of converting it twice.

var tempDateConverted = Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime"));
 newObj.LineCheckSubmitDateTime = tempDateConverted == DateTime.MinValue ?
                                    null : (DateTime?) tempDateConverted;

You can also explicitly cast null to DateTime? in the above statement.

I am not sure about the LineCheckSubmitDateTime control in GridView, here is a possibility that its value is already a DateTime object , returned inside an object. You can also try:

object obj = gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime");
newObj.LineCheckSubmitDateTime = (DateTime?) obj;

With the above code, you don't have to call Convert.ToDateTime.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • @EhsanAkbar, made a mistake in code, instead of `DateTime?` I had `DateTime`. Code is modified now. It should be fine. – Habib Aug 02 '16 at 17:27
  • Habib unfortunately i have same error do you change the code? – Ehsan Akbar Aug 02 '16 at 17:31
  • You shouldn't get that error again, and yes I have changed the code. – Habib Aug 02 '16 at 17:33
  • In fact i exactly copied you code into my vs,but i have the same error . – Ehsan Akbar Aug 02 '16 at 17:36
  • comment out these lines in VS. clean , Rebuild your code, once successfully build, then uncomment these lines in your code and try again. There could be an issue with the visual studio build. – Habib Aug 02 '16 at 17:37
  • Now it works without syntax error but i run it again and the obj variable initialized with null and in the next line i get null reference error – Ehsan Akbar Aug 02 '16 at 17:41
  • @EhsanAkbar, try `newObj.LineCheckSubmitDateTime = (DateTime?) obj;` – Habib Aug 02 '16 at 17:42