Nullable types and datareader...

  • Thread starter Thread starter MobileBoy36
  • Start date Start date
M

MobileBoy36

Hi all,

Nullable types were announced as new handy stuff in .NET 2.0
But it seems like the datareader doesn't support nullable types.
You have still to check for "IsDbNull".
So, are Nullable Types useful at this moment? Do you get any benefit using
them (at this moment)?
Is Microsoft going to change this?

Best regards,
Mobile Boy
 
Nullable types were announced as new handy stuff in .NET 2.0
But it seems like the datareader doesn't support nullable types.
You have still to check for "IsDbNull".
So, are Nullable Types useful at this moment? Do you get any benefit
using them (at this moment)?
Is Microsoft going to change this?

I don't think DBNull and Null are the same thing - that's why Nullable
types are not directly converted.

Take a look at LLBLGen Pro - it's a database framework from .NET. It has
support for Nullable types and the like... and MORE :)
 
Here's an example of using a nullable type. They are
good to use when you need to know if a number is 0 or Null,
because those are two different things, especially if you
are doing numerical calculations.

Dim IntegerData As Nullable(Of Integer)

If IntegerData.HasValue Then
Console.WriteLine("IntegerData = " & IntegerData.ToString)
Else
Console.WriteLine("IntegerData = Null.")
End If

If you read data from a database, you have to check for DBNull
and act accordingly.

IntegerData = Nothing
If NOT IsDBNull(myRow.ProductCode) Then
IntegerData = myRow.ProductCode
End If

Another syntax for this is:
If myRow.ProductCode IsNot DbNull.Value Then
...

Robin S.
 
Mobile Boy,
In addition to the other comments.

I find nullable types to be very useful on my domain (business) objects.

My data mappers (data objects) use DataReader.IsDBNull to decide if they
need to return Nothing or a value for the nullable types on the domain
objects.
 
Back
Top