简体   繁体   中英

Windows Service gives String was not recognized as a valid DateTime exception but the same code running properly in Console Application

Windows Service gives String was not recognized as a valid DateTime exception but the same code running properly in Console Application

Object max = cmd.ExecuteScalar();   //max will have 6/30/2012 12:00:00 AM 
DateTime currentDt = DateTime.Now;
currentDt = DateTime.ParseExact(max.ToString(), "M/d/yyyy h:mm:ss tt", CultureInfo.CurrentCulture.DateTimeFormat);                         //This Line Gives Error in WindowsService Only
StreamWriter sw = new StreamWriter("E:\\ram\\SampleService.txt", true);
sw.WriteLine(currentDt.ToString());
sw.Close();

I even Changed System DateTime Format Settings to Engish - Us Settings.ShortDatetime is M/d/yyyy and Longtime is h:mm:ss tt.

Can someone help me resolve this issue?

My guess is that the system locale isn't the same as your user locale. If your system locale uses something other than "/" as the date separator, it will fail to match the "/" in your format string.

I suggest you change to use CultureInfo.InvariantCulture , at which point it should work - if the value of max.ToString() is actually "6/30/2012 12:00:00 AM". Have you validated that in the case it's failing, that's the value you're getting?

If your value is coming from a database though, why is it stored as a string to start with? Are you sure it even is a string? If it's actually a DateTime , then when you call ToString() you'll be using the current culture's default format to convert it - which could easily fail on the way back. Even if it is a string at the moment, does it really have to be? The fewer string conversions you can introduce, the better.

(As an aside, it's simpler to use File.WriteAllText or File.AppendAllText than using a StreamWriter like this. If you do need to use a StreamWriter , remember to use a using statement to dispose of the resource properly.)

在没有 CultureInfo 的情况下尝试:

currentDt = DateTime.ParseExact(max.ToString(), "M/d/yyyy h:mm:ss tt", null);

Does your SQL request return DateTime or String value?

Also I don't see why do you use ParseExact in this case. In your case I would suggest to use Parse method with InvariantCulture parameter:

currentDt = DateTime.Parse(max.ToString(), CultureInfo.InvariantCulture);

If your max value is DateTime then you don't need to do parsing at all. You just need to check if it is DBNull.Value:

if (max != DBNullValue)
{
    currentDt = (DateTime)max;
}

I got same problem when i had been building windows service.In that, i tryed to convert string date to datetime format. For that i used format string date_time = Day + "/" + Mn + "/" + Yr + " " + Hr + ":" + Min; and tryed convert it to datetime(). This format was running properly in windows form application but not in Windows service project.

Solution :

I changed above string format with this format string date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min; , And then passed it to datetime(). Like follow :

string date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min; DateTime dt_1 = Convert.ToDateTime(date_time);

By default the en-US culture is used by .NET according to which the Date is in Month/Day/Year format

This is 100% working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM