uwking Posted March 22, 2004 Posted March 22, 2004 <SIZE=5> Hello gurus, I have been lookin at this for like 6 hours now, and I am turnin to you guys... This below is the error I get <B> --- ERROR BEGINS --- </B> 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 248: Line 249: string myDateTimeValue = "2/16/1992 12:15:12"; Line 250: DateTime myDateTime = DateTime.Parse (myDateTimeValue); Line 251: Line 252: Source File: c:\inetpub\wwwroot\agentaccountingapp\agent\store_usage.aspx.cs Line: 250 Stack Trace: [FormatException: String was not recognized as a valid DateTime.] System.DateTimeParse.GetDayOfNNY(DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi) +202 System.DateTimeParse.ProcessTerminaltState(Int32 dps, DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi) +142 System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +488 System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) +30 System.DateTime.Parse(String s, IFormatProvider provider) +11 System.DateTime.Parse(String s) +7 AgentAccountingApp.Agent.store_usage.lstBoxWeek_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\agentaccountingapp\agent\store_usage.aspx.cs:250 System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +108 System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +26 System.Web.UI.Page.RaiseChangedEvents() +115 System.Web.UI.Page.ProcessRequestMain() +1081 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 <B> --- ERROR ENDS --- </B> By the way, that code is strictly taken from msdn site... There seems to be nothing wrong with the code itself nor import problems... I'm totally lost...HELP!!! </SIZE> Quote
Moderators Robby Posted March 22, 2004 Moderators Posted March 22, 2004 (edited) what data type is 'myDateTimeValue' [edit]Disregard what I said, I see that it's a string :) Edited March 22, 2004 by Robby Quote Visit...Bassic Software
MorningZ Posted March 22, 2004 Posted March 22, 2004 what about using "CType(string,datetime)" instead? Quote If you make it idiot proof, they'll build a better idiot :-)
Administrators PlausiblyDamp Posted March 22, 2004 Administrators Posted March 22, 2004 try System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true); string myDateTimeValue = "2/16/1992 12:15:12"; DateTime myDateTime = DateTime.Parse (myDateTimeValue,format); their code is assuming US date / time format. If you are using different regional settings then this will cause your problem. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PhilBayley Posted March 22, 2004 Posted March 22, 2004 It has something to do with globalization and I think it is expecting a US formatted date because it works fine with string myDateTimeValue = "16/2/1992 12:15:12"; try setting the IFormatProvider part of the Parse function. Quote
PhilBayley Posted March 22, 2004 Posted March 22, 2004 Just noticed - of course "2/16/1992" wont work as there are only 12 months in the year. The example is in a US format so just change your string to be UK format and you wont need to add the globalization (unless your from the US). ;-) Quote
uwking Posted March 22, 2004 Author Posted March 22, 2004 Thanks a bunch!!! That fixed it, thanks PlausiblyDamp!!! :D try System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true); string myDateTimeValue = "2/16/1992 12:15:12"; DateTime myDateTime = DateTime.Parse (myDateTimeValue,format); their code is assuming US date / time format. If you are using different regional settings then this will cause your problem. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.