Solved

if selecteddate is null?

Posted on 2007-11-19
8
429 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Footer for each row on Gridview 2 41
looking for an ASP.net report designer 4 42
asp web application 3 43
asp.net repeater 2 15
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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