locked
Nullable Objects must have value for DateTime RRS feed

  • Question

  • User1208695299 posted

    Hi friends..

    @oned_gk @dotnetzoom
      How to pass Null value to database? In my codebehind page

    I have tried this code but its not working for me

     Dim dateSample As System.Nullable(Of Date) = Date.Now
     Dim Datestring As String = dateSample.Value.ToString("d")
     dateSample = Nothing

    Ex: Classname.Update(Txtbox1.text, dateSample)

    When i try to pass this datesample in update function getting error like this "Nullable Objects must have value"

    Date data type is DateTime.. m using SQL server 2008

    Tuesday, April 29, 2014 1:42 AM

Answers

  • User1401801381 posted

    How to pass Null value to database? In my codebehind page

    Hi

    when you want to insert a null value in a database:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2014 2:01 AM
  • User-1716253493 posted

    Try like this

    cmd.Parameters("@date").Value = IIf(dateSample.HasValue, dateSample.Value, DBNull.Value)

    or try

            If dateSample.HasValue Then
                cmd.Parameters("@date").Value = dateSample.Value
            Else
                cmd.Parameters("@date").Value = DBNull.Value
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2014 3:57 AM

All replies

  • User1401801381 posted

    How to pass Null value to database? In my codebehind page

    Hi

    when you want to insert a null value in a database:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2014 2:01 AM
  • User-1716253493 posted

    Try like this

    cmd.Parameters("@date").Value = IIf(dateSample.HasValue, dateSample.Value, DBNull.Value)

    or try

            If dateSample.HasValue Then
                cmd.Parameters("@date").Value = dateSample.Value
            Else
                cmd.Parameters("@date").Value = DBNull.Value
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2014 3:57 AM