0

I have a gridview with a boundfield that contains DateTime columns. In the OnRowDataBound event I check if that date is NOT NULL and depending on the result I do some action.

if (!String.IsNullOrEmpty(e.Row.Cells[16].ToString()))

BUT, even though the cell is NULL - would it be empty the date would be displayed as 1.1.1900 - the condition is always fulfilled!

I did some workaround replacing the null values with '' (empty) and therefore changed the condition into

if (!e.Row.Cells[16].Text != "01/01/1900 0:00:00")

This works, but then I have all this irrelevant data displayed in the gridview.

What's wrong?


I was asked to clarify what had been the problem for future users who might step on this thread:

Imagine you have this SQL query:

select getdate() as date
union all
select NULL

the data returned is:

2015-12-22 12:58:37.650
NULL

if you display this data in a gridview and check its values in the OnRowDataBound event, then you step on the issue I had: In order to find those rows that do not have a NULL entry you CAN NOT do:

if(e.Row.Cells[12].Text != null)

it does not work. The "nothing" that is displayed in the gridview cell in this case has 6 characters (don't ask my why - this is pure empiric) and therefore can be filtered by

if(e.Row.Cells[12].Text.Length != 6) 

I implemented this in my solution, but now as do write this, I come to the conclusion that this all could have been avoided by simply specify the correct default date ("1900-01-01") in the SQL Union expression. Actually I think I stepped on something incoherent in SQL. In the above Query the NULL shouldn't be returned, as the type of the column definitely is datetime. Hope now everybody is fine with this comment. Martin

Barnabeck
  • 459
  • 1
  • 6
  • 26
  • 4
    If it's not a nullable datetime it'll fill in the default value – Colm Prunty Dec 09 '15 at 13:00
  • 4
    your second if statement should not even compile. – M.kazem Akhgary Dec 09 '15 at 13:00
  • 3
    This seems like a really weird way to check if something is null: if (!String.IsNullOrEmpty(e.Row.Cells[16].ToString())). Use e.Row.Cells[16] != null instead. No need for needles conversion to string. – Ivan Dec 09 '15 at 13:02
  • @Colm Prunty: where does it fill in the the default value? In the gridviews boundfield those records with NULL are just empty. – Barnabeck Dec 09 '15 at 14:27
  • @M.kazem Akhgary: in fact it does compile and it seems I have to get back to this workaround as nothing said here helped me out. – Barnabeck Dec 09 '15 at 14:31
  • @Ivan: I had tried that in the first place, but it doesn't work either. Keep in mind that I don't have a datetime at that point; I retrieve it from the gridview (as text) and convert it to DateTime later. – Barnabeck Dec 09 '15 at 14:33
  • You have something in there, calling ToString on a null will result in NullReferenceException. If the code is executing inside some WPF event, WPF will silently catch and handle exception. What objects do you have in those cells? – Ivan Dec 09 '15 at 16:32

1 Answers1

0

What ever u have done , from My point of view that partially correct. but instead of using that hardcoded value use can check it with DateTime.MinValue.Like my bellow code. I haven't tested it. but I think this will work.

if (e.Row.Cells[16]!= DateTime.MinValue))

DateTime field has no way to be Null, because its Value type, So it is stored as DateTime.Min . So we can add trailing question mark(?) to transform the DateTime to a nullable DateTime.Then your Code will work .

DateTime? someDate = DateTime.MinValue;

        if(someDate != null)
        {
            //Do you CODE
        }
manas das
  • 64
  • 12
  • I think `default(DateTime)` is more semantically meaningful than `DateTime.MinValue` – sara Dec 09 '15 at 13:28
  • I agree that DateTime can't be NULL. I built that gridview with lines having a date and others that do not. How do I check in the code behind whether there is a date or not? If there is anything, then I can convert the content to date and do whatever I want DateTime Check = Convert.ToDateTime(e.Row.Cells[16]); but this would logically throw an error if applied to an empty cell. – Barnabeck Dec 09 '15 at 14:45
  • Solved: string check = Convert.ToString(e.Row.Cells[16].Text to find out how the content of that empty NULL Gridview - cell is processed. The result is a non-visible, 6 character long string. Taking advantage of it's length I adapted the if condition to if (e.Row.Cells[16].Text.Length != 6) to get the non-NULL lines. Empiric Solution – Barnabeck Dec 09 '15 at 15:43
  • What ever you had mentioned in your question, i suggested a solution for that, now i am not sure what exactly is you requirement .. If you can please edit your question, then someday it may help someone else to find his solution in it. – manas das Dec 10 '15 at 19:43
  • manas das. I hadn't seen your comment. Hope that my comments clarified the issue. Martin – Barnabeck Dec 22 '15 at 12:24
  • When ever there is null value present in row .. that will be represented as ** ** , So why aren't we checking the cells value with ** ** in condition. thats why you are getting 'e.Row.Cells[12].Text.Length ' as 6 right. – manas das Dec 24 '15 at 15:51