G Nell
G Nell

Reputation: 85

Operator '<=' is not defined for type 'Date' and type 'DBNull'

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

Upvotes: 0

Views: 672

Answers (2)

Terry Carmen
Terry Carmen

Reputation: 3896

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.

Upvotes: 0

China Syndrome
China Syndrome

Reputation: 993

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

Upvotes: 1

Related Questions