Handling Null Value in DB Column

G

Guest

I have a column of a database that can return a Null value instead of a string.
I have a class that holds the database row, and I have tried to add a
property to handle the null but the program it blows up before it even gets
there.

For example:

given a class with a field called name with this property:
Public Property name() As String
Get
Return pvt_name
End Get
Set(ByVal name1 As String)
If name = Is DBNull.Value Then
pvt_name = space(30)
Else
pvt_name = name
End If
End Set
End Property

and a datareader column called drinst("Name")

newrec.name = drinst("Name") blows up if name is nulls. It doesn't
even get into the property statement.

Is there a way to automatically handle a null value without having to create
an IF statement for each column to check for it?
 
G

Guest

A string value cannot be DBNull. So, an expression like "name = Is
DBNull.Value"
(did you really mean "name1 Is DBNull.Value"?) is not valid.

Database types (like those derived from OleDBType or SQLType) can be null,
but primitive types, like string, can't.

The trouble is the type that is returned by an expression like:
drinst("Name") varies, depending on whether the column value retrieved from
the database is null. It will be either DBNull, or some other type,
depending on the column definition in the DB.

So, your PROPERTY cannot be defined as a primitive type like string.
Try something like this:

Public Property name() As Object
Get
Return pvt_name
End Get

Set(ByVal Value as Object
If Value DBNull.Value Then
pvt_name = space(30)
Else
pvt_name = name
End If
End Set
End Property

The type of variable pvt_name, of course, will have to match the datatype
for the column, when it is not null.

HTH
 
G

Guest

Hi,

Also try setting the NullValue property of the field in the datatable schema
to (Empty).

HTH,
Rakesh Rajan
 

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