473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DBNull.Value Question

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
11 4898
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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
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.nosp am> wrote in message
news:eo******** ******@TK2MSFTN GP14.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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
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.nosp am> wrote in message
news:eo******** ******@TK2MSFTN GP14.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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
Patrick,
I still think we are getting hung in up our terminology...U nit isn't a
column , it's a specific row value of a column.....sayi ng 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.Dat aColumn)

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...thoug h 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.Va lue)

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

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O. Ige" <Pa*********@di scussions.micro soft.com> wrote in message
news:F9******** *************** ***********@mic rosoft.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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
ok Karl sorry for the misunderstandin g.
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...U nit isn't a
column , it's a specific row value of a column.....sayi ng 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.Dat aColumn)

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...thoug h 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.Va lue)

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

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O. Ige" <Pa*********@di scussions.micro soft.com> wrote in message
news:F9******** *************** ***********@mic rosoft.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*********@di scussions.micro soft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
> I still think we are getting hung in up our terminology...U nit isn't a
column , it's a specific row value of a column.....sayi ng 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.Dat aColumn)


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
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...U nit isn't a
column , it's a specific row value of a column.....sayi ng 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.Dat aColumn)


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
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*********@di scussions.micro soft.com> wrote in message
news:A5******** *************** ***********@mic rosoft.com...
ok Karl sorry for the misunderstandin g.
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...U nit isn't a
column , it's a specific row value of a column.....sayi ng 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.Dat aColumn)

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...thoug h 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.Va lue)

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

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Patrick.O. Ige" <Pa*********@di scussions.micro soft.com> wrote in message
news:F9******** *************** ***********@mic rosoft.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*********@di scussions.micro soft.com> wrote in
message > news:77******** *************** ***********@mic rosoft.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1490
by: Filipe Cristóvão | last post by:
Hi, I know that this is a newbie's question, but I need to know how i do to know if a field in a table is DBNull. If I try to write (with response.write) the field, it gives me an error: "Cast from type 'DBNull' to type 'String' is not valid". Thanks for any help. -- Filipe Cristóvão
4
1725
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 several other things. How is it done?
4
14917
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value, DBNull.Value), DateTime), & ...
4
5752
by: Hon Yuen, Ng | last post by:
Hi This is a newbie question. I've a datatable that contains null value. Sometime i need to store this data into different variables. However, error will be thrown if the data i tried to stored in the variable is a null value. E.g. Dim int As Int64 Dim null As Object
10
2408
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
6
4611
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 DBNull.Value)) andalso (newPosition.ReportsTo <> 0) then The first time I did it I used "and" and got the following error. Operator is not valid for type...
19
19317
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
8
13928
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal = SCOPE_IDENTITY(); Pretty simple. There is an additional TransmissionID column that is an autonumber and primary key. @retVal is always null in my...
3
6283
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate NullValue in my datagridview... Everything works great, with a table-row added manually to the database, without setting any value to the second...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.