|
-
Aug 26th, 2011, 08:50 AM
#1
Thread Starter
PowerPoster
[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]
-
Aug 26th, 2011, 08:55 AM
#2
Re: default date vs null
Why not try the easy way and do
Code:
Else
sqlinsert2.Parameters.AddWithValue("@CONFIRMDATE", "None")
-
Aug 26th, 2011, 09:01 AM
#3
Re: default date vs null
Hi,
You can find some information, here.
-
Aug 26th, 2011, 09:04 AM
#4
Thread Starter
PowerPoster
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]
-
Aug 26th, 2011, 09:11 AM
#5
Re: default date vs null
 Originally Posted by Pasvorto
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
-
Aug 26th, 2011, 09:18 AM
#6
Thread Starter
PowerPoster
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]
-
Aug 26th, 2011, 09:22 AM
#7
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
-
Aug 26th, 2011, 09:23 AM
#8
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.
-
Aug 26th, 2011, 09:26 AM
#9
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|