locked
nullable object must have value in vb.net -please help me friends RRS feed

  • Question

  • User-916396441 posted
    Hi friends,
    I am  getting "nullable object must have value".How can i resloved the issue. i am new to .net
    The code shown below
     
    InDto
    Public Property FromDate As Nullable(Of Date)<o:p></o:p>
    Public Property ToDate As Nullable(Of Date)

    Public Shared Function ValidateBizRulXXX(dto As DTO.NameofDTO) As Boolean

            Dim isValid As Boolean

            Dim Datevalidate As New DateValidattion

     If Datevalidate.CompareDate(dto.FromDate.Value, dto.ToDate.Value, DateValidattion.DateValidation.Date1GreaterThanorEqualtoDate2, 0) OrElse

      Datevalidate.CompareDate(dto.FromDate.Value, Date.Today, DateValidattion.DateValidation.Date1GreaterThanDate2, 0) OrElse

       Datevalidate.CompareDate(dto.ToDate.Value, Date.Today, DateValidattion.DateValidation.Date1GreaterThanDate2, 0) Then

         BrokenRulesContext.GetBrokenRulesFromContext.AddBrokenRule(New BrokenRule(BIZRUL_VIOLATION_MESSAGE_ABCDE))

                        isValid = False

                    Else

                        isValid = True

                    End If

            End If

            Return isValid

        End Function


    Friday, August 31, 2012 7:56 PM

Answers

  • User-158764254 posted

    You cannot call this: dto.FromDate.Value

    If dto.FromDate is null.

    If you attempt to read the Value property of a Nullable when that Nullable is still null, then you wil get the error you're seeing.

    You can check the HasValue property before trying to read the Value

    http://msdn.microsoft.com/en-us/library/sksw8094.aspx


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 1, 2012 1:45 PM

All replies

  • User-158764254 posted

    you're attempting to read the Value property of FromDate and ToDate but you never assigned a value to either of them.

    Friday, August 31, 2012 10:23 PM
  • User-916396441 posted

     If Datevalidate.CompareDate(dto. FromDate.Value, dto. ToDate.Value, DateValidattion.DateValidation.Date1GreaterThanorEqualtoDate2, 0) OrElse

                      Datevalidate.CompareDate(dto. FromDate.Value, Date.Today, DateValidattion.DateValidation.Date1GreaterThanDate2, 0) OrElse

                Datevalidate.CompareDate(dto. ToDate.Value, Date.Today, DateValidattion.DateValidation.Date1GreaterThanDate2, 0) Then

                        BrokenRulesContext.GetBrokenRulesFromContext.AddBrokenRule(New BrokenRule(BIZRUL_VIOLATION_MESSAGE_adbc))

                        isValid = False

                    Else

                        isValid = True

                    End If

                End If

            End If

     

     

    Indto:

       Public Property FromDate As Nullable(Of Date)
       Public Property ToDate As Nullable(Of Date)

     

     

    InASPX  Page

     <ABC:wPanel ID="Date" CssClass="KeepInLine" runat="server">
                    <ABC:DateTextBox ID="FromDate" runat="server" Caption="From" CssClass="dateControlSmall"
                        ControlRowCssClass="KeepInLine" />
                    <ABC:wDateTextBox ID="ToDate" runat="server" Caption="To" CssClass="dateControlSmall"
                        ControlRowCssClass="KeepInLine" /><ABC:wPanel>
    Saturday, September 1, 2012 1:24 AM
  • User-158764254 posted

    You cannot call this: dto.FromDate.Value

    If dto.FromDate is null.

    If you attempt to read the Value property of a Nullable when that Nullable is still null, then you wil get the error you're seeing.

    You can check the HasValue property before trying to read the Value

    http://msdn.microsoft.com/en-us/library/sksw8094.aspx


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 1, 2012 1:45 PM