Results 1 to 9 of 9

Thread: [RESOLVED] default date vs null

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved [RESOLVED] default date vs null

    I have the following code:
    Code:
     If Trim(txtConfirmationDate.Text) <> "" Then
         sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", CDate(txtConfirmationDate.Text))
    Else
         sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", VB.vbNull)
    End If
    Unfortunately, it writes "01/01/1900" to the database. How do I get it to just leave the field as NULL?
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: default date vs null

    Why not try the easy way and do
    Code:
    Else
        sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", "None")

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: default date vs null

    Hi,

    You can find some information, here.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: default date vs null

    Hack: The field in the db is set up as a date format. Won't "NONE" give me a type mismatch error?
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: default date vs null

    Quote Originally Posted by Pasvorto View Post
    Hack: The field in the db is set up as a date format. Won't "NONE" give me a type mismatch error?
    Then drop the Else clause and let the database handle it.
    Code:
    If Trim(txtConfirmationDate.Text) <> "" Then
         sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", CDate
    End If

  6. #6

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: default date vs null

    Due to how the Insert command is constructed, wouldn't I have to account for that field?
    Code:
    sqlinsert2.CommandText = "INSERT INTO POHEADER ([PONUM],[PODATE],[REVISION],[REVDATE],[PAGE],[FREIGHT],[BUYER],[BPHONE],[BEMAIL],[BFAX],[VENDOR1],[VENDOR2],[VENDOR3],[VENDOR4],[CONTACT],[ITEMS],[STATUS],[CUSTOMER],[SALESREP],[COMMENTS],[FOB],[CONFIRMDATE]) "
    
    sqlinsert2.CommandText &= "VALUES (@PONUM,@PODATE,@REVISION,@REVDATE,@PAGE,@FREIGHT,@BUYER,@BPHONE,@BEMAIL,@BFAX,@VENDOR1,@VENDOR2,@VENDOR3,@VENDOR4,@CONTACT,@ITEMS,@STATUS,@CUSTOMER,@SALESREP,@COMMENTS,@FOB,@CONFIRMDATE)"
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: default date vs null

    Try this
    Code:
    Dim confirmationDate As Date
    If Date.TryParse(txtConfirmationDate.Text.Trim, confirmationDate) Then
        sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", confirmationDate)
    Else
        sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", DBNull.Value)
    End If
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: default date vs null

    vbNull returns nothing, which for dates will give you their default value.

    Perhaps you want to use DBNull.Value?

    Or a nullable datetime instance.

  9. #9

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: default date vs null

    I will give that a try, Wild Bill. Thanks all.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

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