Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 1.0
This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2004, 12:45 PM
Authorized User
 
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date/Time Still getting error

Hello,

Here is my code:
 Dim valDOB As DateTime = CType(objRecruit.CheckDOB(txtDOB.Text), Date)

   objRecruit.DOB = valDOB.Date
   Response.Write(valDOB.Date)

Here's the Function Code:
Function CheckDOB(ByVal DOB As String) As DateTime

        Dim thedate As String
        thedate = Format(DOB, "mm/dd/yyyy")
        Try
            thedate = Format(Convert.ToDateTime(DOB), "mm/dd/yyyy")
            Return CType(thedate, Date)
        Catch ex As Exception
            Return CType(thedate, Date)
        End Try

    End Function

Please someone help...all I am trying to do is error handle, if a user types anything else than the proper date format which is mm/dd/yyyy.

Thanks in Adavnce... P.S. been at this still last Thursday!!

Mark

 
Old October 18th, 2004, 01:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

What error do you ge?

Also, I think your logic is a bit flawed here. A Try Catch is used to catch (un)expected errors, and handle them gracefully. In your situation, you try to format a date. If the format fails, you still try to convert the invalid data to a date in the Catch block.

So, suppose someone types "Bla" as the date in the text field txtDOB. The Try statement fails, and you end up in the Catch statement. The Catch statement then tries to convert Bla to a date, something that won't work.

What exactly are you trying to accomplish? What kind of input can you expect from txtDOB.Text? I think it'll make sense to return an error, or DateTime.MinValue for example when the string cannot be converted to a date, and explain to the user what went wrong.

Also, since CheckDOB returns a date, there is no need to cast (with CType) it to a date anymore in the first line of your code.

If you're worried about incorrect date formats, try three drop downs for the day, week and year element of a date, or look into the <asp:Calendar"> control.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 19th, 2004, 07:20 AM
Authorized User
 
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply...I have it figured out, using the IsDate.

Mark






Similar Threads
Thread Thread Starter Forum Replies Last Post
date and time in EST time zone anboss XSLT 1 May 21st, 2008 01:42 PM
Date-Time Picker Active-X Error Loralee Access 2 January 30th, 2006 08:22 PM
Date/Time Error mrideout BOOK: Beginning ASP.NET 1.0 2 October 18th, 2004 06:37 AM
Microsoft Date and Time Picker - Error mikericc Access VBA 1 December 4th, 2003 04:15 PM
insert system date. no date and time jimmy Access VBA 6 November 20th, 2003 01:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.