Author |
Topic |
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-14 : 14:08:08
|
hello,I have a signup page which is captured by the class below. Locally it runs ok, but when hosted on the remote server. I get the error below.What is wrong here ?ThanksEhiServer Error in '/' Application. --------------------------------------------------------------------------------String was not recognized as a valid DateTime. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: String was not recognized as a valid DateTime.Source Error: Line 25: DateTime dob1 = new DateTime();Line 26: //dob1 = DateTime.Parse(DOB_Day.SelectedValue.ToString() + "/" + DOB_Month.SelectedValue.ToString() + "/" + DOB_Year.SelectedValue.ToString());Line 27: dob1 = DateTime.Parse(DOB_Day.SelectedValue.ToString() + "/" + DOB_Month.SelectedValue.ToString() + "/" + DOB_Year.SelectedValue.ToString()); |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 18:01:00
|
If the SelectedValues are numbers, the date format might be ambiguous. Is 04/12/2008, 12th April or 4th December? That can depend on the server. And if the day is > 12, that can cause an invalid date error.The example on this page tells you about the culture parameter of the Parse function which will help you avoid the issue...http://msdn2.microsoft.com/en-us/library/ey1cdcx8.aspxRyan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-14 : 18:11:15
|
Oh oh,sorry. But i got this issue sorted. I thought i posted that. maybe i should cut back on the coffee.Locally, am running in the UK gmt using dd/mm/yyyy but our server is in the USA using gmt + 7, using mm/dd/yyyySo changing it around works fine.Line 27: dob1 = DateTime.Parse(DOB_Day.SelectedValue.ToString() + "/" + DOB_Month.SelectedValue.ToString() + "/" + DOB_Year.SelectedValue.ToString());is changed toLine 27: dob1 = DateTime.Parse(DOB_Month.SelectedValue.ToString() + "/" + DOB_Day.SelectedValue.ToString() + "/" + DOB_Year.SelectedValue.ToString()); |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 18:13:13
|
Okay. Thanks You're still better off setting the culture so your situation is server independent...Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-14 : 18:32:56
|
Yeah you are right. I could achieve this in classic asp, using the localization. Will have to research it in .netthanks a lot |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-14 : 18:33:31
|
Its 11.33 pm here and am off to bed in 10mins |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 18:35:27
|
Or you could use ISO standard format, YYYYMMDD.Line 27: dob1 = DateTime.Parse(DOB_Year.SelectedValue.ToString() + "-" + DOB_Month.SelectedValue.ToString() + "-" + DOB_Day.SelectedValue.ToString());Line 27: dob1 = DateTime.Parse(DOB_Year.SelectedValue.ToString() + DOB_Month.SelectedValue.ToString() + DOB_Day.SelectedValue.ToString()); E 12°55'05.25"N 56°04'39.16" |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-14 : 18:42:42
|
Oh thanks peter.didnt know you were also a .net programmer. great stuff |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-04-14 : 21:41:04
|
Why are you building and parsing strings into dates? If you have a year, a month, and a day, you just create a new DateTime() with those parameters! Never, ever introduce date formatting and string conversions into the mix when you don't need to!int y = int.Parse(DOB_Year.SelectedValue);int m = int.Parse(DOB_Month.SelectedValue);int d = int.Parse(DOB_Day.SelectedValue);dob = new DateTime(y,m,d);That's it! Keep it simple, and *always* favor date math over string conversions, whether you are coding in .NET or T-SQL or anywhere else.And, of course, watch out for months with less than 31 days and leap years and all that.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-15 : 03:36:51
|
quote: Originally posted by jsmith8858 Why are you building and parsing strings into dates? If you have a year, a month, and a day, you just create a new DateTime() with those parameters! Never, ever introduce date formatting and string conversions into the mix when you don't need to!int y = int.Parse(DOB_Year.SelectedValue);int m = int.Parse(DOB_Month.SelectedValue);int d = int.Parse(DOB_Day.SelectedValue);dob = new DateTime(y,m,d);That's it! Keep it simple, and *always* favor date math over string conversions, whether you are coding in .NET or T-SQL or anywhere else.And, of course, watch out for months with less than 31 days and leap years and all that.- Jeffhttp://weblogs.sqlteam.com/JeffS
Thats very smart and thoughtful.Thanks "ONCE" again.Ehi |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-15 : 08:56:40
|
[code]int y = int.Parse(DOB_Year.SelectedValue); int m = int.Parse(DOB_Month.SelectedValue); int d = int.Parse(DOB_Day.SelectedValue); DateTime dob7; dob7 = new DateTime(y, m, d);Label1.Text = dob7.ToLongDateString();[/code]Perfect. Works like a charm. Thanks a great deal |
|
|
|