Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:07+00:00 2026-06-14T15:22:07+00:00

Windows Service gives String was not recognized as a valid DateTime exception but the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:22:08+00:00Added an answer on June 14, 2026 at 3:22 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are running a .Net 1.1 based Windows Service ( not an ASP.Net application),
I have two instances running of same Windows Service. They check the health of
I have a c# windows service application that is crashing without throwing an exception
I have made a simple windows-service, but when i try to start it it
I want the given application (Windows Service) to act as a remoting server as
I've been given a windows service which references a COM component that only runs
I have a Windows service that writes messages to the Event Log. I also
I want to create windows service which recieves via some queue tasks to run
i have a windows service set to run at certain times, it runs first
To get windows service named MyTestService I create an instance of Win32_Service object then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.