Best way to handle NULLs

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?
 
M

Mark B

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
 
S

Stan

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
 
M

Mark B

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
 
M

Mark B

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
 
A

Andrew Morton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top