Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 More Out of Control

Author  Topic 

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-06-06 : 12:41:53
I have a text field in my app that the user is required to put a date of birth into.

On the same page is a submit button that causes validation of the data entered onto the page using a function I wrote. When it gets to the date entered, if it's invalid I get an unhandled exception (stack overflow)

The code concerned is

Dim dtDob As New Date
Try
dtDob = CDate(Me.txtDoB.Text)
If dtDob < CDate("01/01/1900") Or dtDob > Today() Then

ValidateNewData = False
End If
Catch ex As Exception
ValidateNewData = False
End Try


The line that throws the error is the "dtDob = CDate(Me.txtDoB.Text)" I thought that this error (i.e a daft date of birth) would throw a (handled) exception. What am I missing? Am I going to have to use a calendar control? If so does anyone know of any good (free) ones?

many thanks

steve

-----------

Oh, so they have internet on computers now!

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-06-06 : 13:20:04
That code as writen should hit the CATCH().

Mayb try this instead:

dtDob = CTYPE(Me.txtDoB.Text, Date)

That may work better in this case.

Why not add a regular expression validator to txtDoB?
That would allow you to validate the DOB on the client side before it gets to the server, so that your CDATE will not fail.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-06-06 : 13:27:06
I'd stay away from the old VB functions and try this..

Dim DOB as DateTime
if Not DateTime.TryParse(Me.txtDoB.Text, DOB) then
ValidateNewData = False
Else
if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today Then
ValidateNewData = False
End if

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2006-06-06 : 14:31:31
Why not use 3 drop down selectors. One for month day and year. Then you can build the correct datetime variable out of that.

If you give a user just a text box they could enter anything. "3/6/98", 3/6/1998", 6/3/98, 6-3-98, June 1st 1981, etc... You would then have to interpret the input which is next to impossible.

Just my 2 pennies...

JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
• Position SEO can provide your company with SEO Services at an affordable price
› As far as myself... I do this for fun!
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-06-06 : 14:50:27
Lot of work. You'd want to make the dropdown for the day conditional based on the month and year, or you could still get invalid dates. April 31, February 29, 2003. It is also clunky looking from a user perspective.

Best bet when working with dates is to either invest in a custom control that handles all the shenanigans for you, or simply try and parse what the user sends you. If it's a valid date, it's date. If not, you display an error.

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2006-06-06 : 15:07:44
quote:
If it's a valid date, it's date.


"3/6/98" is a valid date. But is it March 6 or June 3?

JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
• Position SEO can provide your company with SEO Services at an affordable price
› As far as myself... I do this for fun!
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-06-06 : 15:08:47
depends on your culture

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-06-06 : 21:57:08
quote:
Originally posted by dfiala

I'd stay away from the old VB functions and try this..

Dim DOB as DateTime
if Not DateTime.TryParse(Me.txtDoB.Text, DOB) then
ValidateNewData = False
Else
if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today Then
ValidateNewData = False
End if

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP



FYI -- I'm 100% not sure, but I think TryParse() isn't available unless you are using .NET 2.0.
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-06-06 : 22:05:33
You are correct.

In 1.1 you need to do the try yourself...

Dim DOB as DateTime
Try
DOB = DateTime.Parse(Me.txtDoB.Text)

if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today Then
ValidateNewData = False
End if
Catch Ex as Exception
ValidateNewData = False
End Try


Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-06-07 : 08:29:33
Thanks guys for all the advice, much appreciated. I should have said I am using .NET 1.1.

Ideally I want something very quick and easy - so for me at least regular expressions are out as I've never really got to grips with them.

thanks

steve

-----------

Oh, so they have internet on computers now!
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-06-07 : 08:40:28
Regex for full date validation gets exceptionally long in order to handle leap years properly.

My previous post showed the simplest way to handle server-side validation in 1.1.

If you want to do client-side check out: http://www.smartwebby.com/DHTML/date_validation.asp

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-06-08 : 10:50:02
I've decided to create my own control

It has 2 drop downs for month and year, and a calendar bit for selecting the date and it can dissappear into an icon. If nothing else it was a useful learning experience creating it.

many thanks to all for your ideas and suggestions

steve


-----------

Oh, so they have internet on computers now!
Go to Top of Page
   

- Advertisement -