Comparison problem

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am trying to see if column forenames is empty/blank in a row in a
datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string "".'
error on the above line. What am I doing wrong?

Thanks

Regards
 
You can not use the "=" operator for checking DBNull. Instead, use:

If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames") Then

-Scott
 
As Scott suggested:

If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value) Then
 
John said:
I am trying to see if column forenames is empty/blank in a row in
a datatable. I am using the following statement to achieve that;

If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value.ToString) Then

I am getting the 'Operator is not valid for type 'DBNull' and string
"".' error on the above line. What am I doing wrong?

If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
 
Hi Paw,

Paw Boel Nielsen said:
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames") Then (suggested by
Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value)
Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
(suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker than
another?)

My is the fastest (2) beacuse it is a simple comparision while (3) is
probably the slowest because of type checking involved.
(1) is in the middle - it is slower than mine because it is a method and it
also checks the value agains null.

My humble opinion :)
 
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames") Then (suggested by
Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") = System.DBNull.Value)
Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull.Value Then
(suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker than
another?)

Paw
 
Paw Boel Nielsen said:
Ok so it seems there are 3 possible ways to check for nulls:

1) If IsDBNull(mydatatable.Rows.Item(0).Item("Forenames") Then
(suggested by Scott M.)
2) If (mydatatable.Rows.Item(0).Item("Forenames") =
System.DBNull.Value) Then (suggested by Miha Markic)
3) If mydatatable.Rows.Item(0).Item("Forenames") Is
System.DBNull.Value Then (suggested by Armin Zingler)

Out of curiosity what are the differences? (Is one way quicker
than another?)

4) If TypeOf mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull


Solution 1) does the same as 4), so 4) is faster because no function call is
involved.

2) is wrong because references have to be compared using the Is operator
 
Miha Markic said:
Hi Paw,



My is the fastest (2) beacuse it is a simple comparision

References have to be compared using the Is operator.
2) leads to a compiler error. Sorry. ;-)
while (3)
is probably the slowest because of type checking involved.

No, it does not check the types, it only compares references.
 
References have to be compared using the Is operator.
2) leads to a compiler error. Sorry. ;-)

You sure on this?
Actually you are right - I've written it in C# and then not complectly
rewritten in VB.NET. Duh.

No, it does not check the types, it only compares references.

Right. I was thinking in C# terms.
 
Hi Armin,

4) If TypeOf mydatatable.Rows.Item(0).Item("Forenames") Is System.DBNull


Solution 1) does the same as 4), so 4) is faster because no function call is
involved.

Yes, it is faster and no it does not the same thing. 1) checks also if value
is null.
Beware that if 1) and 4) do the same then they could be equal in terms of
speed if JIT optimizes 1) to inline calls.
2) is wrong because references have to be compared using the Is operator

Yeah, yeah, VB.NET.
 
Miha Markic said:
Hi Armin,




Yes, it is faster and no it does not the same thing. 1) checks also
if value is null.

Right. I meant "do the same" in the sense of "both compare the types"
(whereas the other one compares references)
Beware that if 1) and 4) do the same then they could be equal in
terms of speed if JIT optimizes 1) to inline calls.

Well, if a function does the same as the same code does outside the
function, they do the same. Of course, I agree, the function call itself
takes additional time.
Yeah, yeah, VB.NET.

:-)
 
Back
Top