DBNull and IsNothing

M

MattB

Hello. I have a vb.net (asp.net) application that uses ado.net datasets.
At one point, I need to check a text field in a DataTable to see if there's
any text in it before performing text operations on that record. I thought I
could just test using something like this:

If Not IsNothing(e.Item.DataItem("guest_name")) Then
....
End If

But it seems a value of DBNull passes this test and evaluates as not
nothing. What would be a better test at this point to see if there's
anything in this field? Thanks!

Matt
 
A

Anon-E-Moose

MattB said:
If Not IsNothing(e.Item.DataItem("guest_name")) Then
...
End If

But it seems a value of DBNull



Why not compare it directly to DBNull?

If Not e.Item.DataItem("guest_name") is DbNull.Value?

I think that works.
 
H

Herfried K. Wagner [MVP]

MattB said:
If Not IsNothing(e.Item.DataItem("guest_name")) Then
...
End If

But it seems a value of DBNull passes this test and evaluates
as not nothing.

\\\
If Not ... Is DBNull.Value Then
...
End If
///
 
M

MattB

Anon-E-Moose said:
Why not compare it directly to DBNull?

If Not e.Item.DataItem("guest_name") is DbNull.Value?

I think that works.

Thanks. When I tried that I got this result, which seems very weird to me.
Hopefully I'm just not fully understanding what's happening here. Any ideas?

Cast from type 'DBNull' to type 'String' is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
type 'String' is not valid.

Source Error:

Line 457: 'split the names into individual links
Line 458: If e.Item.DataItem("Qty") > 1 And Not
e.Item.DataItem("guest_name").GetType Is DBNull.Value Then
Line 459: Dim strName() As String =
Split(e.Item.DataItem("guest_name"), "<br>")
Line 460:
Line 461: cell.Controls.Remove(h)
 
J

Jay B. Harlow [MVP - Outlook]

Matt,
In addition to the other comments I normally use DataRow.IsNull.

If DataItem is a DataRow object, then you should be able to do:

If e.Item.DataItem.IsNull("guest_name") Then

Hope this helps
Jay
 
C

Cor Ligthert

Matt,

I did not see this in the message from Anon
e.Item.DataItem("guest_name").GetType Is DBNull.Value

As well can you better not use it this way of making if statements with
DBnull.value

In my opinion is better
\\\
If Not e.Item.DataItem("guest_name") Is DBNull.Value then
If e.Item.DataItem("Qty") > 1 then
///
Or
\\
If Not e.Item.DataItem("guest_name") Is DBNull.Value AndAlso
e.Item.DataItem("Qty") > 1 then
///
What is the same. ("And" alone goes on evalutating and gives often errors
like you have, what is not in this case as far as I can see).

I hope this helps?

Cor
 
B

Bernie Yaeger

Hi Matt,

If Not irow.IsNull("phone") Then



works fine for me.



HTH,



Bernie Yaeger
 
S

scorpion53061

Bernie,

Don't you think that DbNull and Null will be the paradoxes that someday
tear the fabric of the universe apart?

It is nothing - but it is something.

It has no value - but it is not empty

:)

Anyway good to see you again.
 
B

Bernie Yaeger

Hey Scorp,

Einstein could not deal with it!

:)
scorpion53061 said:
Bernie,

Don't you think that DbNull and Null will be the paradoxes that someday
tear the fabric of the universe apart?

It is nothing - but it is something.

It has no value - but it is not empty

:)

Anyway good to see you again.
 

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

Top