DBNull.Value Question

G

Guest

When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If

I always have 2 returned! Even when Unit is NULL!
I want a case if a column is NULL the it xhould return '1 and if not '2'
How does DBNull.Value work?
 
S

Scott M.

Don't use "IS" when testing against DBNull.Value, use "=".

Use "=" when testing a value against a value and use "IS" when testing an
object against an object.

Or, re-phrase your code to use the IsDBNull() function ---> dim x as
boolean = IsDBNull(Unit)

In your code, is Unit a column object or the value of a column? If you want
to test against DBNull.Value, you need Unit to be the value of a column and
not the column object itself.
 
K

Karl Seguin

Scott:
This is true in C#, but in VB.Net you have to use IS when checking against
DbNull.Value...using = will result in a compilation error.

Patrick:
You say Unit is a column of your table? A column can't be DbNull...only a
specific value of a row. I'd like to see code above this (where Unit gets
set). Also, on a side note, you can rewrite your code like:

If Unit Is DbNull.Value Then
return "1"
end if
return "2"

there's no need for the else, since the first if statement will
automatically break out of the function if true (because of the
return)...but really that's more a style issue :) Anyways, show up more
code :)

Karl
 
G

Guest

Thx for the reply...
What i have is this:-
Protected Function Image(ByVal Unit As Object) As String
If Unit Is DBNull.Value Then
Return "Bad"
Else
Return "Good"
End If
End Function
Unit is a column in my table ie. the unitd contains value " 5 pieced of
bread "
My Question is would i need to use ISNULL in my SQL statement?
 
G

Guest

Yep Karl ur right.. "=" gave me an error in VB.NET!
I have posted the code..
Hope it helps
 
K

Karl Seguin

Patrick,
I still think we are getting hung in up our terminology...Unit isn't a
column , it's a specific row value of a column.....saying its a column
implies that its the entire column entity (for all rows), not for a specific
row....anyways...Scott got confused with this also, namely because in .Net
entire columns can be represented (by System.Data.DataColumn)

You shouldn't need to use IsNull in your SQL and the unit Is DbNull.Value
should work. I'm not sure why it isn't working...though I imagine it has to
do with the actual data...I imagine that it simply isn't null. For example,
try to call the function like:

dim ret as string = Image(DbNull.Value)

I bet anything it'll return "Bad"...

Karl
 
G

Guest

ok Karl sorry for the misunderstanding.
So how can i make the row value actually NULL?
I kept the row value to NULL for testing by just removing the Data in the
row and its blank!
Any help?
 
S

Scott M.

I still think we are getting hung in up our terminology...Unit isn't a
column , it's a specific row value of a column.....saying its a column
implies that its the entire column entity (for all rows), not for a
specific
row....anyways...Scott got confused with this also, namely because in .Net
entire columns can be represented (by System.Data.DataColumn)

Not true, I spaced out on the "=", but I said exactly the same thing as to
the column itself vs. the value of a column in a row:

"In your code, is Unit a column object or the value of a column? If you
want
to test against DBNull.Value, you need Unit to be the value of a column and
not the column object itself.?

-Scott
 
D

Danny Ni

VB.Net has an IsDBNull funtion. The way C# tests for a database null value
does not apply to VB.Net.
 
G

Guest

Scott,
i have sometiing like this :-

Unit
-------
Food
Vegetables
Ice

And for example the table name is UnitFood.

So my ideas was if Vegeatble row is empty i want to return "Bad" for example!
Sorry guys i hope this help!
 
S

Scott M.

A row consists of all the column values in it. You will have to test all 3
columns for values (null) in a given row to see if that row is empty.

A row is an object. It can very well exist (and therefore not be null)
while containing columns that have no values in them. You need to separate
the idea of a row and a column and a column's value in your mind.
 
G

Guest

Thx for the detailed Explanation!
I have fixed my problem...
The NULL wasn't really null
I didn't think about it that A null value is not the same as "zero" or
"blank." NULL means no entry has been made, and usually implies "value
unknown" or "value not applicable."
After i updated the Table with some NULL values it all worked fine!
Thx Karl ,Scott and Danny
Patrick
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top