Best way to handle NULLs

  • Thread starter Thread starter Mark B
  • Start date Start date
M

Mark B

'----------------------------------------------------
If dr("YesNoQuestion") = True And _
dr("StartDate") <= Now And _
dr("EndDate") >= Now Then
Return True
End If
'----------------------------------------------------


I am getting an error if StartDate or EndDate are Null.

What's the appropriate way to handle that here?
 
I think this is the solution, albeit longer winded than something else?

If dr("YesNoQuestion") = True And _
dr("StartDate") IsNot DBNull.Value And _
dr("EndDate") IsNot DBNull.Value Then

If dr("StartDate") <= Now And _
dr("EndDate") >= Now Then
Return True
End If
End If






Mark Rae said:
It depends on what you're trying to do...

Why are the two dates null in the first place?
Unpopulated.


What do you want to happen if they are null?

Nothing

Ignore them in the If statement?

Yes

Replace them with a default value?
No

Something else?
No


If one of them is null, will the other one also be null?
Not necesarily
 
I think this is the solution, albeit longer winded than something else?

 If dr("YesNoQuestion") = True And _
                    dr("StartDate") IsNot DBNull.Value And _
                    dr("EndDate") IsNot DBNull.Value Then

                    If dr("StartDate") <= Now And _
                        dr("EndDate") >= Now Then
                        Return True
                    End If
End If

A more concise way to test for a null value in a DataRow is to use the
IsNull(ByVal ColumnName as string) method i.e.

dr.IsNull("StartDate")

returns true when null
 
Thanks.

I think this is the solution, albeit longer winded than something else?

If dr("YesNoQuestion") = True And _
dr("StartDate") IsNot DBNull.Value And _
dr("EndDate") IsNot DBNull.Value Then

If dr("StartDate") <= Now And _
dr("EndDate") >= Now Then
Return True
End If
End If

A more concise way to test for a null value in a DataRow is to use the
IsNull(ByVal ColumnName as string) method i.e.

dr.IsNull("StartDate")

returns true when null
 
Someone here just gave another solution:

If dr("YesNoQuestion") = True AndAlso_
dr("StartDate") <= Now AndAlso_
dr("EndDate") >= Now Then
Return True
End If

"The use of the AndAlso operator is to test for the existence of an object
instance before attempting to access one of its members."




I think this is the solution, albeit longer winded than something else?

If dr("YesNoQuestion") = True And _
dr("StartDate") IsNot DBNull.Value And _
dr("EndDate") IsNot DBNull.Value Then

If dr("StartDate") <= Now And _
dr("EndDate") >= Now Then
Return True
End If
End If

A more concise way to test for a null value in a DataRow is to use the
IsNull(ByVal ColumnName as string) method i.e.

dr.IsNull("StartDate")

returns true when null
 
Mark said:
Someone here just gave another solution:

If dr("YesNoQuestion") = True AndAlso_
dr("StartDate") <= Now AndAlso_
dr("EndDate") >= Now Then
Return True
End If

"The use of the AndAlso operator is to test for the existence of an
object instance before attempting to access one of its members."

Err... no, that isn't what AndAlso does in general.

AndAlso is used to avoid the need to evaluate every expression. For example,

if A and B and C then.... will evaluate each of A, B and C, even if A
evaluates to false.
if A AndAlso B AndAlso C then... will not evaluate B or C if A evaluates to
false.

It is referred to as "short-circuiting". Also see "OrElse".

You still need to check for null values.

Andrew
 
Back
Top