Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

if selecteddate is null?

vb.net
onClick I pass some form values to my code behind, one of which is a calendar.selecteddate.
If the user leaves this unselected I want to pass the current date to the DB.
I tried
dim StartDate as CalStart.SelectedDate
if StartDate = "" then
StartDate = system.datetime.now
end if

but i get errors converting a datetime to a string (or vice versa)
How can I achieve the above? system.dbnull.value didn't work either
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you have to check what value the field CalStart returns when nothing has been entered.
what is that for a control?
try like this

dim StartDate as  Datetime
StartDate = CalStart.SelectedDate
if StartDate = "" then
StartDate = system.datetime.now


or

dim StartDate as CalStart.SelectedDate
if StartDate = "" then
StartDate = Convert.ToDatetime(system.datetime.now)
Avatar of QPR

ASKER

My question should have read...
dim StartDate as DateTime = CalStart.SelectedDate

Will try your second suggestion when back at work
Avatar of GreymanMSC
GreymanMSC

Simply make sure the .SelectedDate property is a valid date _before_ attempting to assign to a Date typed vatriable.  Initialise with the current
Dim StartDate as Date
If Not IsDate(CalStart.SelectedDate) then
  StartDate = System.DateTime.Now.Date
Else
  StartDate = CalStart.SelectedDate
End If

Open in new window

... Initialise with the current date value if it the selected value not valid (ie, nothing selected).
Avatar of QPR

ASKER

using
Dim StartDate as Date
If Not IsDate(CalStart.SelectedDate) then
  StartDate = System.DateTime.Now.Date
Else
  StartDate = CalStart.SelectedDate
End If

I get the error
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Avatar of QPR

ASKER

using
dim StartDate as date = CalStart.SelectedDate
if StartDate = "" then
StartDate = Convert.ToDatetime(system.datetime.now)

I get the error
Conversion from string "" to type 'Date' is not valid.
ASKER CERTIFIED SOLUTION
Avatar of GreymanMSC
GreymanMSC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial