Solved

if selecteddate is null?

Posted on 2007-11-19
8
423 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

22 Experts available now in Live!

Get 1:1 Help Now