By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

DBNull.Value Question

P: n/a
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?

Nov 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
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?

Nov 18 '05 #2

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <s-***@nospam.nospam> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
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?


Nov 18 '05 #3

P: n/a
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?


"Scott M." wrote:
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
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?


Nov 18 '05 #4

P: n/a
Yep Karl ur right.. "=" gave me an error in VB.NET!
I have posted the code..
Hope it helps

"Karl Seguin" wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <s-***@nospam.nospam> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
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?



Nov 18 '05 #5

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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?


"Scott M." wrote:
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
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?


Nov 18 '05 #6

P: n/a
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?

"Karl Seguin" wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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?


"Scott M." wrote:
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.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
> 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?
>


Nov 18 '05 #7

P: n/a
> 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
Nov 18 '05 #8

P: n/a
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!

"Scott M." wrote:
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

Nov 18 '05 #9

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

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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?

"Karl Seguin" wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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?


"Scott M." wrote:

> 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.
>
>
> "Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in
message > news:77**********************************@microsof t.com...
> > 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?
> >
>
>
>


Nov 18 '05 #10

P: n/a
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.

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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!

"Scott M." wrote:
> 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

Nov 18 '05 #11

P: n/a
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

"Scott M." wrote:
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.

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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!

"Scott M." wrote:
> 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


Nov 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.