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

October 18th, 2004, 12:45 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 18th, 2004, 01:08 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

October 19th, 2004, 07:20 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply...I have it figured out, using the IsDate.
Mark
|
|
 |