0
votes

I have been trying to figure this out. I keep getting an error on DateRcvReal if the cell is empty. I am comparing two fields by using <= and some of the DateRcvReal are empty cells which throws an error of Operator '<=' is not defined for type 'Date' and type 'DBNull' how do I get around this. Still new at VB.net

If e.VisibleIndex > -1 And e.DataColumn.FieldName.StartsWith("DateProm") Then
                If IsDate(CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DatePromReal"})) Then
            If (CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DatePromReal"}) <= CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DateRcvReal"})) Then

                e.Cell.BackColor = Drawing.ColorTranslator.FromHtml("#F42121")
                e.Cell.ForeColor = Drawing.Color.White

            End If
2
Add a check for dbnull and replace the null with a value or whatever your requirements define.Jacob H

2 Answers

1
votes

Try this is not isDBNULL function

IF NOT IsDbNull((CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DatePromReal"})) AndAlso
(CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DatePromReal"}) <= CType(sender, ASPxGridView).GetRowValues(e.VisibleIndex, New String() {"DateRcvReal"})) Then
0
votes

The easiest way is to put your code inside a try/catch block, let it throw the error on the bad comparison, then do whatever is appropriate in the catch block (ignore the error, log it, complain, etc.)

A better way is to test the cell contents for null before using them, and use the catch block to trap truly unexpected conditions.

Also, be careful of comparing booleans with null fields. VB makes assumptions about booleans and nulls so it considers null and false to be equal.

In short, make sure there's a sane value in the cell (not null, not an empty string, not nothing) before trying to compare it to something.