0

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.

3
  • what would you like to read in your Textbox in case of a NULL date? empty string? Commented Aug 21, 2014 at 23:03
  • I was thinking that a null date from the database could be stored as Nothing in the VB.Net object and I thought assigning Nothing to the .Text property of a textbox would be OK. Commented Aug 22, 2014 at 1:31
  • I modified my answer to provide more info. A NULL date can be stored as Nothing, the problem lies in its use for the Textbox field. just use an empty string instead of nothing Commented Aug 22, 2014 at 9:31

1 Answer 1

1

Your AcknowledgeDate is already Nothing, you're setting it yourself in the constructor. You don't need to specify that again in the Set function, in fact you're just saying "if the value is Nothing, then set it as Nothing", which is redundant.

I believe the problem is that you're trying to assign that object Nothing reference to the textbox .Text property and that results in the exception.

TextBox.Text expects a String I don't see the reason of using Nothing. Create a Function to handle the Nothing values and show an empty string instead. Plus, it gives you the freedom of formatting your date as you like.

something like

Function getDateText(ByVal d As Date?) As String
  If d.HasValue = False Then
     Return ""
  End If

  Return d.Value.ToString("D")
End Function

and then call it when you want to set txtAcknowledgeDate.Text

txtAcknowledgeDate.Text = getDateText(thisEvent.AcknowledgeDate)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.