VB.Net >> Nothing vs dbnull.value

by John » Wed, 19 Mar 2008 08:44:15 GMT

Hi

What is the difference between Nothing and dbnull.value?

Thanks

Regards



VB.Net >> Nothing vs dbnull.value

by Steve Gerrard » Wed, 19 Mar 2008 09:27:59 GMT



The important thing is that they are not the same.

Nothing generally refers to an object reference variable that is not a reference
to anything, that is, does not refer to any object.

DBNull.Value is not an object reference, but indicates that a value is Null. As
its name suggests, it usually means that in a database, that particular column,
in some particular row, is Null, even if the column is say an integer column. It
applies to values, not object references.

VB.Net >> Nothing vs dbnull.value

by Cor Ligthert[MVP] » Wed, 19 Mar 2008 13:06:45 GMT

John,

Null tells that there is nothing as the reference to an object (in VB what
is not Ecla is using the keyword as it is "Nothing")
DBNull.Value is a value type (used as a database can return an empty value
and then returns that type)

Both are a kind of legacy from the past. By instance Intel based computers
initialize the declared memory with nulls char(00), other computers do that
with high values char(255). So "null" comes from the hardware and is
practicaly the use of a very low based program language as original C in
fact was. Let say as that we woud use AR for adding two fields (Add
Register) as it was in the beginning even before C.

Cor

"John" < XXXX@XXXXX.COM > schreef in bericht

VB.Net >> Nothing vs dbnull.value

by rowe_newsgroups » Wed, 19 Mar 2008 19:27:02 GMT


As Steve has said, Nothing is basically a state of an object
reference. The reference is either pointing to something or nothing,
so when you are checking for 'is nothing' you are not looking at a
value of an object, but rather it's state. DbNull on the other hand,
does not refer to a state, but a value of an object. Nullable columns
have to be treated special by the database, and that fact must be
handled by .NET. When you receive a value from a database the
reference is pointing to a value, that value being DbNull.

The good news is, that most times you can forgo the search for DbNull.
Especially if you are using a datareader to pull the data, you can
simply do datareader("ColumnName").ToString() and .NET will convert a
DbNull into a blank string ""

Thanks,

Seth Rowe [MVP]

Similar Threads

1. = nothing vs. Is Nothing

Hi guys, quick sanity check please.

When TESTING for nothing, it's "if <expr> Is Nothing"

when ASSIGNING, it's "<expr> = Nothing"

correct ?

so a line "If ThisValue = nothing" in some (inherited) code is a bug, plain
& simple ?

Thanks

2. Null, DBNull, Nothing, VBNullstring - VB.Net

3. DBNULL not really equal to VB.NET NOTHING??

I have a dropdownlist that can't match up a NULL value to entry during
binding giving me this error:

has a SelectedValue which is invalid because it does not exist in the
list of items.
Parameter name: value

I know I can get around the issue by doing a union with a DBNULL in the
column before binding in the .net sqldatasource control. However, I
want to use that datasource for other controls and dont want it to have
a DBNULL value. So I thought the below code would work, since Ive read
that vb's NOthing is equal to DBNULL. Apparently it's not as the error
persist.


Protected Sub AddNull(ByVal sender As Object, ByVal e As
System.EventArgs)
        sender.Items.Insert(0, New ListItem("None", Nothing))
    End Sub

"None" is the text and does show up, but entries with DBNULL don't seem
to match to "Nothing"

Is there some code or value I can put in there that will tie to DBNULL,
or do I need to Cast DBNULL on the way out and on the way up?

4. No rows returned with OleDbParameter.Value = DBNull.Value - ADO.Net

5. Fill-Method changing DBNULL-values to other values

Hi,

i am writing a small interface to import some data from one database to
an other database. unfortunately is the data in the source not very
"nice". you have boolean-columns and those boolean-columns have as a
default value DBNULL.
it wouldn't be a problem if there was one boolean field, i would have a
if (table.iscolumnNull()) but i have about 70 boolean fields and only
ONE is true all  others are false. that means i have many of those ifs
already

if (table.column1)
{
}
else if (table.column2)

the problem is, i don't want to have a structure like this:

if (table.column1IsNull())
{
    if (table.column1){}
}
else if (table.column2IsNull())
 ....

I hope you can see where I am going.
So I think there must be a possibility to change the DBNULL-values
within the fill-method of the table-adapter.
something like
select
case
    when column1=null then column1=false
    else column1
endcase
from foreigntable

i have tried to write such a statement but it didn't work out. If
somebody had some hint, that'll be great! 

Greetings
Chris

6. problems with parsing & format in ConvertEventArgs when field value is DBNULL (without HTML formating) - VB.Net

7. How to easily parse DBNull value into other data type

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
  null = DBNull.Value
  int = null 'this will throw error

To solve this, i have to check if the value is DBNull each time. Is 
there a simpler way to do this?

Thanks in advance.

From,
Hon Yuen, Ng

8. database INSERT choking on dbNULL values - VB.Net