Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a Null value for a date field

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
Does anyone know how i can insert a null value for a date. My code looks like this:

Dim dtEndDate As Date
If txtEndDate.Text = String.Empty Then
dtEndDate = DBNull.Value
^DOES NOT LIKE THIS ^
^SAYS VALUE OF SYSTEM.DBNULL CANNOT BE CONVERTED TO DATE^
^I MUST INSERT A NULL IF ETXTBOX IS EMPTY ^
Else
dtEndDate = txtEndDate.Text
End If

INSERT INTO PROCESS_PERIOD (EndDate) VALUES ( '" & dtEndDate & "')"

Any help with this would be greatly appreciated
 
Try

dtEndDate = Cstr(dtEndDate)

Then insert dtEndDate.

Make sure the data columns allows nulls.

Finally, it might not be the most elegant way, but have 2 insert strings, one for when dates aren't null and one for when they are. When they aren't null, just don't pass anything. Should work if your table column for the date field allows nulls.
 
DateTime (aka Date) does not accept a DbNull. You should test for null, and assign DateTime.MinValue when loading the DateTime variable, and when going to write it to the database, do the reverse (Check for MinValue, and assign DbNull)

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top