DBNull

G

Greg

I have a DataTable that I'm assigning values from. I have an Integer value
that happens to contain a null, thus I cannot assign the Null value to my
Class Property.

I have the following lines of code and neither works.

If NOT String.IsNullOrEmpty(oUser.Rows(0).Item("DeptID")) Then
Me.DeptID = CType(oUser.Rows(0).Item("DeptID"), Integer)
End If

I'm doing something wrong here. What I need to do is examine the
oUser.Rows(0).Item("DeptID") to make sure its not a NULL value. Thus, if it
has a value then i will assign the value.

Any assistance with this problem will be greatly appreciated.

Thanks,
Greg
 
A

Andrew Backer

DBNull.Value

You should be testing for DBNull.Value when you pull it out of the datatable.
This is one of the ugly routines that we use, but I know it could be improved
=) It only handles Strings, and I know I have written much more robust generic
versions, but I can't seem to find 'em now.

Private Shared Function IfNull(ByVal obj As Object, Optional ByVal def As
String = "") As String
.....If (obj Is Nothing) Or (obj Is DBNull.Value) Then Return def
.....Return CStr(obj).Trim()
End Function

' just returns Nothing if the value is DBNull.Value. In the case of value
types (int/etc) this
' will equate to whatever Nothing is for them (0 for int, etc)
Public Shared Function dbnz(ByVal obj As Object) As Object
.....If (obj.Equals(DBNull.Value)) Then Return Nothing
.....Return obj
End Function

Hope this helps,

//Andrew
 
A

Armin Zingler

Greg said:
I have a DataTable that I'm assigning values from. I have an Integer
value that happens to contain a null, thus I cannot assign the Null
value to my Class Property.

I have the following lines of code and neither works.

If NOT String.IsNullOrEmpty(oUser.Rows(0).Item("DeptID")) Then
Me.DeptID = CType(oUser.Rows(0).Item("DeptID"), Integer)
End If

I'm doing something wrong here. What I need to do is examine the
oUser.Rows(0).Item("DeptID") to make sure its not a NULL value.
Thus, if it has a value then i will assign the value.

Any assistance with this problem will be greatly appreciated.

if oUser.Rows(0).Item("DeptID") is DBNull.Value then


But this won't help you much because your class' property is still
Integer. You can make use of a typed DataSet or change the class'
property type to Nullable(Of Integer) (short VB 2008: "As Integer?") .
The latter approach requires conversion from DBNull.Value to Nothing
when setting the property, and vice versa when storing the value back in
the DataRow.


Armin
 
H

Herfried K. Wagner [MVP]

Greg said:
I have a DataTable that I'm assigning values from. I have an Integer value
that happens to contain a null, thus I cannot assign the Null value to my
Class Property.

I have the following lines of code and neither works.

If NOT String.IsNullOrEmpty(oUser.Rows(0).Item("DeptID")) Then

=> 'If oUser.Rows(0).Item("DeptID") IsNot DBNull.Value Then' or 'If Not
IsDBNull(oUser.Rows(0).Item("DeptID")) Then'.
 
C

Cor Ligthert[MVP]

Greg-

What do you mean with null, because that has a lot of meanings and inside VB
luckily only the stuppid word nullable, what was a failure. Null is a word
like Light in Coca Cola, you are free to call everything Light even me but I
like more my correct name.

To repeat again about the word "Null" that became at a certain time as a
kind of cult word (before that time was used "High Values").

Null means in C type languages: there is nothing set as reference with
referense types

DBNull.value is the value type that is used in an object as the receiving
field is confirm that type NULL in SQL.

NULL means in SQL server that the veld is empty (surely no nulls) it means
in fact Empty, but I have seen that those guys at the
SQL server group like it more to use their own cult words instead of
meaningfull ones. You see that often with devellopers who are afraid not to
be seen for full.

Nothing can mean VB type languages: there is nothing set as value with value
types and therefore the default value (this does not exist in C type
languages as a value type had always to be instanced with a value)

Nothing means can also mean in VB type languages: there is nothing set as
reference with reference types and therefore referencing to nothing.

Dim X as object 'here the there is nothing referenced
X = DataTable.Rows(0).items(0) 'here X can be contain any value including
DBNull.Value but normally you know the type as it is not DBNull.Value. The
server can set to NULL allowed and then it is the given ValueType or the
DBNull.Value

if X is not DBNull.Value then 'if the other possible type inside the object
is integer
if CInt(X) = nothing ' then you know that it is zero, although this one
will probably never be used in this case

I hope this gives an idea

-Cor
 

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

Top