|
-
Nov 13th, 2005, 06:59 PM
#1
Thread Starter
Junior Member
String was not recognized as a valid DateTime
Dear All,
I have this piece of code:-
CType(r.FindControl("calDate"), Calendar).SelectedDate = DateTime.Parse(CType(r.FindControl("txtEvDate"), TextBox).Text())
And its giving me an error:-
String was not recognized as a valid DateTime
How can I solve this?
Thanks for your help and time
Johann
-
Nov 13th, 2005, 07:44 PM
#2
Re: String was not recognized as a valid DateTime
You can make sure that the txtEvDate control contains a valid date. DateTime.Parse only recognises certain date formats and will throw an exception if it doesn't find one. If you're using a specific date format then you can use ParseExact. If you're using .NET 2.0 you can use TryParse. Otherwise you need to validate the value in the TextBox. Don't just assume that the user will enter the correct value. They could enter "purple monkey dishwasher" if they wanted. I'm sure you won't be surpridsed to learn that that wouldn't parse to a date.
-
Nov 13th, 2005, 08:10 PM
#3
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
I tried to go around it like this:-
If Request.QueryString("Cmd") = "Add" Then
CType(r.FindControl("calDate"), Calendar).SelectedDate = DateTime.Today.ToShortDateString
Else
CType(r.FindControl("calDate"), Calendar).SelectedDate = CType(r.FindControl("calDate"), Calendar).SelectedDate.ToShortDateString
End If
And the "Add" is working fine, displaying today's date. However when I come to edit, the following error is cropping up
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
-
Nov 13th, 2005, 08:38 PM
#4
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
I was inspecting a bit with the debugger.
The problem seems to arise when there is a value in the date bigger than 12.
For example, 11/06/2005 works fine
However 14/11/2005 does not.
Have no idea why it is not accepting that value
-
Nov 13th, 2005, 08:42 PM
#5
Re: String was not recognized as a valid DateTime
Presumably Date.Parse is assuming that the first number is the month. I'm not sure but I'd expect that its behaviour is determined by the current culture setting on the local machine. If that's not the case then you'll have to use ParseExact and tell it exactly what format to use.
-
Nov 13th, 2005, 08:49 PM
#6
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
Can you tell me how to do that since I have never done it before
Thanks for your help
-
Nov 13th, 2005, 09:00 PM
#7
Re: String was not recognized as a valid DateTime
There are a number of ways. I suggest you take a look at the appropriate MSDN topics for more info, as you always should when dealing with unfamiliar types or members. MSDN won't tell you everything but it should always be your first port of call. In this case, if you know you want dd/mm/yyyy format then the easiest way is to use:
VB Code:
Dim myDate As Date = Date.ParseExact(myString, "dd/MM/yyyy", Nothing)
-
Nov 13th, 2005, 09:05 PM
#8
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
I tried this code:-
Dim myDate As DateTime = DateTime.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "dd/MM/yyyy HH:mm", New DateTimeFormatInfo)
CType(r.FindControl("calDate"), System.Web.UI.WebControls.Calendar).SelectedDate = myDate.ToShortDateString
-
Nov 13th, 2005, 09:11 PM
#9
Re: String was not recognized as a valid DateTime
hey about the culture thing Jm,
know of any way to detect the culture setting? so that i can make my app issue a warning to the user?
you see parseExact is all good, but the date picker boxes i have on some forms would still look funny, hehe
-
Nov 13th, 2005, 09:14 PM
#10
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
I am trying to figure out this whole globalization thing cause I have never used it!
-
Nov 13th, 2005, 09:25 PM
#11
Re: String was not recognized as a valid DateTime
 Originally Posted by Phill64
hey about the culture thing Jm,
know of any way to detect the culture setting? so that i can make my app issue a warning to the user?
you see parseExact is all good, but the date picker boxes i have on some forms would still look funny, hehe 
Look at the CultureInfo class and its CurrentCulture and CurrentUICulture properties.
-
Nov 13th, 2005, 09:39 PM
#12
Thread Starter
Junior Member
Re: String was not recognized as a valid DateTime
ok i solved it
I had to do the format like this:-
Dim myDate As DateTime = DateTime.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "MM/dd/yy", New DateTimeFormatInfo)
and now its workin!
I know this is not the best format but I am doing some amendments on an old system, and I wish to keep the same format it originally uses.
Thanks for your help!
Johann
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|