Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

if selecteddate is null?

Posted on 2007-11-19
8
Medium Priority
?
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

596 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