locked
Passing NULL dates through a property RRS feed

  • Question

  • User-49466106 posted

    Hi;

    Where do I validate my date values prior to passing them as insert parms ?

    I am trying to pass some Null dates.

    In my property ?

     

     Public Property BirthDate As Date
            Get
                Return m_BirthDate
            End Get
            Set(ByVal value As Date)
                HERE ??  
                m_BirthDate = value
            End Set
        End Property
     
    Or just prior to creating my insert parm ?
     
        HERE ???
      InsertCommand.Parameters.AddWithValue("BirthDate", estateRec.BirthDate)
     
    The value "#time#" is getting passed right now.
    So I am getting a SQLDateTime overflow message.
     
    Thanks for your insights !
     

     
     
     


     

     

    Tuesday, January 11, 2011 6:54 PM

Answers

  • User653228039 posted

    Yeah, DateTime is a struct, so it doesn't allow null values.  I usually use the static value "DateTime.MinValue" for my initialized values.


    Or, if you really want to use null, you can look at this article:

    http://dotnetperls.com/nullable-datetime

    I find it complicates things, because you have to keep using the coallesce operator (??).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 11, 2011 7:11 PM

All replies

  • User653228039 posted

    Yeah, DateTime is a struct, so it doesn't allow null values.  I usually use the static value "DateTime.MinValue" for my initialized values.


    Or, if you really want to use null, you can look at this article:

    http://dotnetperls.com/nullable-datetime

    I find it complicates things, because you have to keep using the coallesce operator (??).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 11, 2011 7:11 PM
  • User-49466106 posted

    Hi;

    I found this code for VB in a book.

    This is another way of passing null dates from a class property.

     

    Step 1. define your classes' private var as type NUllable(of Date). 

    Step 2. make your property type Nullable(Of Date) too. 

    Public Property Birthdate As Nullable(Of Date)
            Get
                If m_Birthdate.HasValue Then
                    Return CDate(m_Birthdate)
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As Nullable(Of Date))
                m_Birthdate = value
            End Set
        End Property


     

    Wednesday, January 12, 2011 6:56 PM