473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 17467
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BillG | last post by:
This is my code. If the value coming in from the database table is null, I wish to place a 0 in the column or otherwise the value itself. Everything I try this code I get an error. Am I doing...
4
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and...
12
by: Steve Peterson | last post by:
Hi - just a quick question. I was wondering which is the better "VB.Net" way - to use IsNothing or the Is Nothing in an If..Then statement For example: If IsNothing(myObject) then 'blah...
4
by: tinman | last post by:
Hi.... There appears to be another way of checking if an object IsNothing in ..NET.....was wondering if this approach is better than the classic VB6 way of checking Is Nothing ? Example...
10
by: Bob | last post by:
I'm sure there's a good reason, I just haven't been able to think of it - why didn't MS allow "DBNull" values to simply be a null (Nothing)? Bob
4
by: Dave Taylor | last post by:
This is really not very important but something that I'm just curious about...which is faster to execute or are they the same: (1) If IsNothing(_myVar) Then ... or (2) If _myVar Is Nothing...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
3
by: Larry | last post by:
Hi all, I am coming over from VB 6, learning VB.NET. I have read and have in front of me, the language reference from the msdn, on; Behavior of Null has changed, Isdbnull function and...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.