Stan Bibbs

Stan Bibbs

  • NA
  • 1
  • 4.9k

Nullable

Dec 24 2010 8:19 AM

I am experiencing an error message when I attempt to unpack a row from a datatable which includes a column value which is null.  I am working in VB 2005 and using SQL Server 2005 as my back end.
The table has several date fields, some of which always have data in them and some of which may have a null value depending upon whether the data is available when the record is created.  The app permits the user to leave certain date fields empty and the data can be input later, in a different session perhaps by a different person.
The procedure I have coded uses a With statement to unpack the database row into a custom object.  The members of my custom object which represent the dates where null values may occur have been defined as nullable types (see example below):
    Private mdtmActualFinishDate As Nullable(Of DateTime)
    Public Property ActualFinishDate() As Nullable(Of DateTime)
        Get
            Return mdtmActualFinishDate
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            mdtmActualFinishDate = value
        End Set
    End Property
The code which seeks to cast the row data into the object's property is as follows:
objJob.ActualFinishDate = CType(.Item(CN_ActualFinishDate), Nullable(Of DateTime))
I receive an invalid cast error when my procedure seeks to execute this line.  Isn't the whole point to nullable types the flexibility of holding null values?  What am I missing?