Results 1 to 3 of 3

Thread: [RESOLVED] NULL DATETIME & SQL database

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Location
    Phalaborwa, South Afirca
    Posts
    48

    Resolved [RESOLVED] NULL DATETIME & SQL database

    Hi,
    I am trying to get to grips with the “NULL” dates in SQL database.

    Q1) The PrepareDateForDb code works OK on an update of a record, but when I try add a record with a null date it throws an exception?

    Q2) The date time when I retrieve the datetime stored in the database as ToOADate format, the date time is a day out. I assume as I use a UK date time it is placing my date two hours behind. My customers may not have the same datetime location.

    Thanks

    This works OK
    Code:
    strSQL = "UPDATE [Business] SET" & _
    ...
    " ,DateVerified = " & PrepareDateForDb(mtbDateVerified.Text) & _
    ...
    This throws an exception
    Code:
    ...
    With cmSQL
    ...
    .Parameters.Add(New SqlParameter("@DateVerified", SqlDbType.DateTime)).Value = PrepareDateForDb(mtbDateVerified.Text)
    Code:
    ‘ Prepare date for inserting into database
        Function PrepareDateForDb(ByVal strValue As String) As String
            'Empty MaskedTextBox field gives “  /  /”
            If strValue.StartsWith(" ") Then
                Return "NULL"
                Exit Function
            End If
            'Empty date field
            If strValue.Trim() = "" Then
                Return "NULL"
            Else
                'Convert date to ToOADate 
                Dim strDt As String = CDate(strValue).ToOADate
                Return strDt
            End If
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: NULL DATETIME & SQL database

    You're specifying that your parameter is type DateTime yet you're assigning a String object to it. If the parameter is type DateTime then you need to assign a DateTime value to it. Also, a String containing "NULL" is absolutely NOT a null value. It is a string containing four characters.
    vb.net Code:
    1. Dim dt As Date
    2.  
    3. If Date.TryParse(mtbDateVerified.Text, dt) Then
    4.     cmSQL.Parameters.AddWithValue("@DateVerified", dt)
    5. Else
    6.     cmSQL.Parameters.Add("@DateVerified", SqlDbType.DateTime).Value = DBNull.Value
    7. End If
    Better still, use a DateTimePicker control for dates and/or times. If you need to be able to specify null values then set its ShowCheckBox property to True:
    vb.net Code:
    1. If myDateTimePicker.Checked Then
    2.     cmSQL.Parameters.AddWithValue("@DateVerified", myDateTimePicker.Value.Date)
    3. Else
    4.     cmSQL.Parameters.Add("@DateVerified", SqlDbType.DateTime).Value = DBNull.Value
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    May 2007
    Location
    Phalaborwa, South Afirca
    Posts
    48

    Resolved Re: NULL DATETIME & SQL database

    Excellent thanks,
    I will have to use the first option.

    Thanks again; what would I do with out you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width