C# .NET - Input string was not in a correct format

Asked By Tejaswini Prashant J on 23-Jul-09 07:15 AM
Hi, In a windows form I have code ..
try
{
string _tempTotalTime;
TimeSpan _totalTime = DateTime.UtcNow - Convert.ToDateTime("2026-07-23 22:21:48.220");
//2009-07-23 22:21:48.220 is a variable value which I am retriving from database

_tempTotalTime = _totalTime.Hours.ToString() + ":" + _totalTime.Minutes.ToString() + ":" + _totalTime.Seconds.ToString();

_totalTime = TimeSpan.Parse(_tempTotalTime);

label1.Text = _totalTime.ToString();
}

catch (Exception ex)
{
label2.Text = ex.StackTrace + ex.InnerException + ex.Message + ex.Source;
}
 When I run this form, i get the expected output.
 Now I created a setup project of the same application which contains only this code. The setup is running proper and giving the proper input , but......... when I run this setup on the different machine (which is not a development machine ..but having 2.0 framework installed) it gives me error..

ex.StackTrace -> at System.TimeSpan.StringParser.Parse(String s)
ex.InnerException -> at System.TimeSpan.Parse(String s)
 ex.Message-> at ... Input string was not in a correct format
ex.Source -> mscorlib

Can anybody help me out? ???? it's urgent

Locale - Adam Houldsworth replied to Tejaswini Prashant J on 23-Jul-09 07:37 AM

Hi,

What are the locale of the two machines?

Conversions of DateTime values take into account the current computer locale for globalization.

For example, my locale is en-GB so I can parse en-GB correct dates.

The second computer you run this on could be a different locale, therefore the time you are giving it is not in the correct format for that country/language.

Adam

Hi - Tejaswini Prashant J replied to Adam Houldsworth on 23-Jul-09 07:47 AM

Hi,
thanks for a quick reply ..
but I want my application run on any machine..
So what should I implement to avoid this locale?
Vasanthakumar D replied to Tejaswini Prashant J on 23-Jul-09 07:51 AM

Hi,

its not a culture specific issue.

you are getting negative values for Hours, minute and second when the utc time is less than the date you have given.

so, try this...

_tempTotalTime = System.Math.Abs(_totalTime.Hours).ToString() + ":" + System.Math.Abs(_totalTime.Minutes).ToString() + ":" + System.Math.Abs(_totalTime.Seconds).ToString();

Adam Houldsworth replied to Tejaswini Prashant J on 23-Jul-09 09:05 AM

Looking at your code it seems to me that you are trying to format the output of the TimeSpan in a particular manner.

Look at the ToString method, it has overloads for accepting format strings - this means you can completely avoid your parsing methods as they would become superfluous.

For example: _span.ToString("hh:mm:ss");

HTH,

Adam

Nevermind - Adam Houldsworth replied to Adam Houldsworth on 23-Jul-09 09:10 AM

TimeSpan.ToString does not have those overloads, only DateTime does.

You can print the string you want using string.Format:

string.Format("{0}:{1}:{2}", _span.Hours, _span.Minutes, _span.Seconds);

And still avoid the parsing.

Adam

Thnks .. - Tejaswini Prashant J replied to Vasanthakumar D on 24-Jul-09 05:30 AM

Yes.. the major issue was with the negative values..thnks a lot ur help :) made my day ...
Thnks - Tejaswini Prashant J replied to Adam Houldsworth on 24-Jul-09 05:31 AM

Hi,
my main problem was showing negative values... but after setting that got the locale diff also..
thnks for ur help
tlamy tlamy replied to Tejaswini Prashant J on 11-Mar-12 12:42 AM
hi  i solved this problem


in my computer the program take the datetime(time) in this format "13:00" and in other computer take this same datime(time) like this "13:00 PM" when i try to subtract the program throws this error when trying to print
"input string was not in a correct format" the problem is the "am" and "pm"


the solution is:
string tmpDateNow=DateTime.Now.ToShortDateString();
string[] DateNow=tmpDateNow.Split('/'); 


string tmpTimeNow=DateTime.Now.ToShortTimeString();
string[] TimeNow=tmpTimeNow.Split(':',' ');//separates the "am·" and "pm" in an hour


//like this TimeNow[0]-->hh TimeNow[1]-->mm TimeNow[3]-->AM or PM
//in the remaining squatters who do not have the "am" or "pm"


DateTime TNow=new DateTime(Convert.ToInt32(DateNow[2]),Convert.ToInt32(DateNow[1]),Convert.ToInt32(DateNow[1]),
Convert.ToInt32(TimeNow[0]),Convert.ToInt32(TimeNow[2]),0); 
//new DaTime(year,moth,day,hour,minutes,seconds);


 
DateTime timeOld = new DateTime(2009,07,23,22,21,48);
//TimeSpan _totalTime = DateTime.UtcNow - Convert.ToDateTime("2026-07-23 22:21:48.220");


TimeSpan _totalTime =new TimeSpan(TNow.Ticks -timeOld.Ticks) ;


 
label1.Text = _totalTime.ToString(@"dd\+hh\:mm"); 


 
 
bit late but I hope this helps you or someone else having the same problem 
tlamy23 says: good luck