Date form - check for null

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
I have a form that has a date/time picker on it. When a date is picked a variable is set to the value. (dStartdate = StartDate.Text)
The next time I call the SetDate form (from the same calling form) I want to have it start with the date last chosen. (StartDate.Text = dStartdate). If I call the SetDate form from a different calling form I want the default date to be something else.

First time through I want it to start with Today's date. (dim dt As DateTime = DateTime.Now)

1. How do I check if dstartdate is null? - first time date form is called.

2. How do set dstartdate to null when the calling form closes.

I use the SetDate form throughout the project to set report date parameters. Different Forms call the same SetDate form and require different default dates to be set... (eg first day of current month, last day of the month etc.) All this can be done by trying to keep track of another boolean field (saveDate = true) but I want ideas for a better way.

Thanks for listening.
 
Dates and DateTimes are value types and therfore cannot be set to null. If you set a Date object to Nothing you will find that its value becomes the minimum date possible, which is 12:00 AM on Jan 1st, 0001. You would have to test for this date.
 
So, what do we do when trying to pass a value to an SQL database.

I have a Nullable datetime that can take NULL values.
How do I set my MaskedTextBox to Null if the value is ""

Right now, when I try to do an Insert() function, and one of the parameters is my empty date from the maskedtextbox, the value is " / /", and the error is that I can't convert this string to a date.

So, I do some code to get the value to "", instead of " / /", and I get the same error..."Can't convert "" string to date"

I have to have a NULL date in these cases, so how do I get a NULL value to go over to the SQL table? I tried datepicker and that doesn't even allow for entries of NULL. And it's produces a string as well.
 
Don't use the maskedTextbox since it isn't really needed.

This is how I check to see if my calendar object has a valid DateValue.

VB.NET:
Dim thedate as string = ""
thedate = calOrderDate.SelectedDate
If thedate = "12:00:00 AM" Or Len(Trim(thedate)) = 0 Then
     'pass NULL in your SQL
Else
     'pass thedate
End If

To set the value of the date.
VB.NET:
'SET TO TODAY
calDisplay.SelectedDate = Today()
 
'SEt to Value for Datagrid edit.
If isDate(e.Item.CElls(2).Text) Then
    calDisplay.SelectedDate = CType(e.Item.Cells(2).Text, Date)
End IF
 
'Set to VALUE from Database
If NOT isDBNULL(reader("calDate")) Then
    calDisplay.SelectedDate = CType(reader("calDate"), Date)
End If
 
But will the calendar object allow for a null value?
And what about times only that are still the datetime type in sql?
 
the Calendar object will not allow NULLs However You can choose to never set the .SELECTEDDATE property of the object and it will default to "12:00:00 AM". Then when reading it you can do the check I sent above.

Unless there is a new feature in SQL2005 then datetime fields will automatically put "01/01/0001" as the date portion of any datetime not containing one. SO If you get a valid datetime from the database then before loading that into the calendar object's .SELECTEDDATE property you should just check to see if the DATE portion of the field is '01/01/0001' or in my case
---CType(reader(1), Date) = #12/30/1899 12:34:45 AM# ---
because I have "Y2K safe" turned on in my database. The field actually only holds "12:34:45 PM" but when sql returns the value I get the above.

Obviously you will need to do some run throughs of your data and see how TIME only data is really being perceived/treated by your datasource. Then before you try and set the .SELECTEDDATE property of your calendar object check for thoose cases.

Sorry there is no easy answer to this... ONE THING I really wish we had was a TIME object associated but seperate from our DATE object. Just for this very reason.
 
Back
Top