Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. (SOLVED) Error: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

(SOLVED) Error: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Scheduled Pinned Locked Moved ASP.NET
helpdatabasedebuggingtutorialquestion
4 Posts 3 Posters 21 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Greetings again dear experts, Back for more your great assistance.

    // check to be sure the date selected is not ouside acceptable boudaries
    DateTime runDate = DateTime.Parse(rsRunDate.Text);

    When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:

    DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);

    However, I still get same error. I tried the following:

    DateTime _runDate;
    string runDate = "";

    _runDate = DateTime.Parse(srRunDate.Text);
    runDate = _runDate.ToString("dd/MMM/yyyy");

    But then, the following produces an error:

    if (runDate.Date > DateTime.Now.Date)...

    Now, I get the following error:

    The name 'runDate' does not exist in the current context

    I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?

    D L 2 Replies Last reply
    0
    • S samflex

      Greetings again dear experts, Back for more your great assistance.

      // check to be sure the date selected is not ouside acceptable boudaries
      DateTime runDate = DateTime.Parse(rsRunDate.Text);

      When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:

      DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);

      However, I still get same error. I tried the following:

      DateTime _runDate;
      string runDate = "";

      _runDate = DateTime.Parse(srRunDate.Text);
      runDate = _runDate.ToString("dd/MMM/yyyy");

      But then, the following produces an error:

      if (runDate.Date > DateTime.Now.Date)...

      Now, I get the following error:

      The name 'runDate' does not exist in the current context

      I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You shouldn't be looking at the variable that hold the parsed value. The text hasn't been parsed, so what's the point? You should be looking at the content of srRunDate.Text. For example, trying to use Parse on an empty string will throw an exception. You should change your code to use TryParse instead. At least then, you'll get a return value of true or false denoting if the parse operation worked or not.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You shouldn't be looking at the variable that hold the parsed value. The text hasn't been parsed, so what's the point? You should be looking at the content of srRunDate.Text. For example, trying to use Parse on an empty string will throw an exception. You should change your code to use TryParse instead. At least then, you'll get a return value of true or false denoting if the parse operation worked or not.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        S Offline
        S Offline
        samflex
        wrote on last edited by
        #3

        Thank you very much Dave.

        1 Reply Last reply
        0
        • S samflex

          Greetings again dear experts, Back for more your great assistance.

          // check to be sure the date selected is not ouside acceptable boudaries
          DateTime runDate = DateTime.Parse(rsRunDate.Text);

          When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:

          DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);

          However, I still get same error. I tried the following:

          DateTime _runDate;
          string runDate = "";

          _runDate = DateTime.Parse(srRunDate.Text);
          runDate = _runDate.ToString("dd/MMM/yyyy");

          But then, the following produces an error:

          if (runDate.Date > DateTime.Now.Date)...

          Now, I get the following error:

          The name 'runDate' does not exist in the current context

          I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          "{" is not part of a date. The exception is correct.

          Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups