Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Help with Convert.ToDateTime(String) error


Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2006, 05:12 AM
cosmosis's Avatar
cosmosis cosmosis is offline
Centurion
 
Join Date: Jan 2004
Location: norway
Posts: 156
Unhappy Help with Convert.ToDateTime(String) error


I'm just so confused here..

I run my application, do some stuff, call a sub which contains this line:
Code:
aParallel.StartDate = Convert.ToDateTime(stringReportStart)
...and everything works fine

However, the NEXT time this sub is called, with the EXACT same data input, an exception (described below) is throwed on the line above. Why? It worked the first time!?

If I exit the program and run it again - same thing; it works the first time, but next time it's all baluba.

Please help


Code:
************** Exception Text **************
System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.GetDayOfNNN(DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi)
   at System.DateTimeParse.ProcessTerminaltState(Int32 dps, DateTimeResult result, DateTimeRawInfo raw, DateTimeFormatInfo dtfi)
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles)
   at System.DateTime.Parse(String s, IFormatProvider provider)
   at System.DateTime.Parse(String s)
   at System.Convert.ToDateTime(String value)
....
Reply With Quote
  #2  
Old 08-28-2006, 06:03 AM
Cags's Avatar
Cags Cags is offline
Contributor

* Expert *
 
Join Date: Jul 2003
Location: Leicestershire, England
Posts: 710
Default

Assumably the value of stringReportStart is being changed between the first running of the sub and the second running of the sub. If stringReportStart is not a valid DateTime then your code will crash.
__________________
I don't suffer from insanity, I enjoy every minute of it.
http://www.tib-studios.co.uk
Reply With Quote
  #3  
Old 08-28-2006, 06:25 AM
cosmosis's Avatar
cosmosis cosmosis is offline
Centurion
 
Join Date: Jan 2004
Location: norway
Posts: 156
Default

well, this was the first thing i checked, and i can assure you, the data is indentical the first and second time. any other ideas?
Reply With Quote
  #4  
Old 08-28-2006, 07:22 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

it sounds like code somewhere else is modifying your date. (clearing it out or something like that.) If you're truely calling it with the exact same string, it won't error.

Are you using .NET 2.0 or 1.1? If you're using 2.0, Look at the DateTime.TryParse funciton. That will automatically wrap your conversion in a Try..Catch block. I would also use DateTime.Parse instead of Convert.ToDateTime() anyhow... (regardless of version.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->