I have a SQL Server table with several date columns that can be NULL and in fact are NULL.
I thought I was handling this correctly by using the .HasValue test before trying for the value of a nullable type. But apparently not because I'm getting an InvalidOperationException - Nullable object must have a value - when the getter in my class object tries to populate a textbox:
Public Class EventItem
Private _AcknowledgeDate As Nullable(Of Date)
Public Property AcknowledgeDate() As Date?
Get
Return _AcknowledgeDate
End Get
Set(ByVal value As Date?)
If value.HasValue Then
_AcknowledgeDate = value
Else
_AcknowledgeDate = Nothing
End If
End Set
End Property
my constructor gets data from SQL server; here is the fragment from constructor:
Me.AcknowledgeDate = If(IsDBNull(theRdr("AcknowledgeDate")), Nothing, theRdr("AcknowledgeDate"))
When the above setter runs the date is Null and the private variable _AcknowledgeDate is set to Nothing.
When the code tries to assign something to the textbox, the getter code gets the exception here:
txtAcknowledgeDate.Text = thisEvent.AcknowledgeDate
I can make assign the textbox as follows: txtAcknowledgeDate.Text = Nothing
Despite reading quite a bit about this nullable type facility, I guess I am quite confused about how to implement nullable dates in VB.Net.
NULLdate? empty string?