Solved

if selecteddate is null?

Posted on 2007-11-19
8
416 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:QPR
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20311478
you have to check what value the field CalStart returns when nothing has been entered.
what is that for a control?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20311536
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)
0
 
LVL 29

Author Comment

by:QPR
ID: 20311580
My question should have read...
dim StartDate as DateTime = CalStart.SelectedDate

Will try your second suggestion when back at work
0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20316386
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

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20316401
... Initialise with the current date value if it the selected value not valid (ie, nothing selected).
0
 
LVL 29

Author Comment

by:QPR
ID: 20322574
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.
0
 
LVL 29

Author Comment

by:QPR
ID: 20322604
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.
0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 20323769
Well, "" cannot be compared with a date value without a conversion.  Because the null string in the RHS of the comparision, it is implicitly converted to the same type as the LHS (a Date type, which errors).  Instead, either place the null string on the LHS, or explicitly convert everything to strings (using StartDate.ToString).
 
However, on closer examination of the Calendar control, when unselected the .SelectedValue is actually assigned to #1/01/0001 12:00:00 AM# rather than "" or Nothing.  So the comparison will fail anyway.
Dim StartDate As Date = System.DateTime.Now.Date

If Not CalStart.SelectedDate.Equals(Nothing) Then

  StartDate = CalStart.SelectedDate.Date

End If

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now