Solved

if selecteddate is null?

Posted on 2007-11-19
8
432 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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