469,362 Members | 2,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

DBNull and IsNothing

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
Nov 21 '05 #1
8 17044
"MattB" <so********@yahoo.com> wrote in news:2t*************@uni-berlin.de:
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.
Nov 21 '05 #2
"MattB" <so********@yahoo.com> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
Anon-E-Moose wrote:
"MattB" <so********@yahoo.com> wrote in
news:2t*************@uni-berlin.de:
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.


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)
Nov 21 '05 #4
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

"MattB" <so********@yahoo.com> wrote in message
news:2t*************@uni-berlin.de...
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

Nov 21 '05 #5
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

"MattB" <so********@yahoo.com> schreef in bericht
news:2t*************@uni-berlin.de...
Anon-E-Moose wrote:
"MattB" <so********@yahoo.com> wrote in
news:2t*************@uni-berlin.de:
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.


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)

Nov 21 '05 #6
Hi Matt,

If Not irow.IsNull("phone") Then

works fine for me.

HTH,

Bernie Yaeger

"MattB" <so********@yahoo.com> wrote in message
news:2t*************@uni-berlin.de...
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

Nov 21 '05 #7
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.

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:#h**************@TK2MSFTNGP09.phx.gbl:
Hi Matt,

If Not irow.IsNull("phone") Then

works fine for me.

HTH,

Bernie Yaeger

"MattB" <so********@yahoo.com> wrote in message
news:2t*************@uni-berlin.de...
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


Nov 21 '05 #8
Hey Scorp,

Einstein could not deal with it!

:)
"scorpion53061" <ad***@nospamherekjmsolutions.com> wrote in message
news:O0**************@TK2MSFTNGP14.phx.gbl...
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.

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:#h**************@TK2MSFTNGP09.phx.gbl:
Hi Matt,

If Not irow.IsNull("phone") Then

works fine for me.

HTH,

Bernie Yaeger

"MattB" <so********@yahoo.com> wrote in message
news:2t*************@uni-berlin.de...
> 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
>

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by BillG | last post: by
4 posts views Thread by Tina | last post: by
12 posts views Thread by Steve Peterson | last post: by
4 posts views Thread by tinman | last post: by
10 posts views Thread by Bob | last post: by
4 posts views Thread by Dave Taylor | last post: by
6 posts views Thread by tshad | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.