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. General Programming
  3. .NET (Core and Framework)
  4. String was not recognized as a valid DateTime.

String was not recognized as a valid DateTime.

Scheduled Pinned Locked Moved .NET (Core and Framework)
com
21 Posts 6 Posters 0 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.
  • A abdul subhan mohammed

    Line 260: newCandidate.FirstNameAR = txtFirstNameAR.Text.Trim();
    Line 261: newCandidate.LastNameAR = txtLastNameAR.Text.Trim();
    Line 262: newCandidate.DOB = Convert.ToDateTime(txtDOB.Text);
    Line 263: newCandidate.Address = txtAddress.Text;
    Line 264: newCandidate.CountryID = Convert.ToInt16(ddlCountry.SelectedValue);

    Click here, for more details[^]

    S Offline
    S Offline
    Slacker007
    wrote on last edited by
    #4

    I would not use Convert.ToDateTime with a text/string input. Instead try using TryParse with datetime. DateTime.TryParse Method (String, DateTime) (System)[^]

    realJSOPR L C 3 Replies Last reply
    0
    • A abdul subhan mohammed

      Line 260: newCandidate.FirstNameAR = txtFirstNameAR.Text.Trim();
      Line 261: newCandidate.LastNameAR = txtLastNameAR.Text.Trim();
      Line 262: newCandidate.DOB = Convert.ToDateTime(txtDOB.Text);
      Line 263: newCandidate.Address = txtAddress.Text;
      Line 264: newCandidate.CountryID = Convert.ToInt16(ddlCountry.SelectedValue);

      Click here, for more details[^]

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #5

      Actually (to build on Slacker's answer), the return value of the TryParse method is either true or false, and if true, the specified out parameter contains the parsed DateTime value. If the return value is false, the specified string was not in a recognizable DateTime string format. Additionally, if you know for sure what the format of the string will be, you can use ParseExact. For details about using either of these methods, google is, once again, your friend.

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      S 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Actually (to build on Slacker's answer), the return value of the TryParse method is either true or false, and if true, the specified out parameter contains the parsed DateTime value. If the return value is false, the specified string was not in a recognizable DateTime string format. Additionally, if you know for sure what the format of the string will be, you can use ParseExact. For details about using either of these methods, google is, once again, your friend.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        S Offline
        S Offline
        Slacker007
        wrote on last edited by
        #6

        Thanks, I corrected my post. Funny, I use this all the time, yet I posted its implementation incorrectly. :doh: :-\

        1 Reply Last reply
        0
        • S Slacker007

          I would not use Convert.ToDateTime with a text/string input. Instead try using TryParse with datetime. DateTime.TryParse Method (String, DateTime) (System)[^]

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #7

          You can't set a DateTime to null. :)

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          S 1 Reply Last reply
          0
          • realJSOPR realJSOP

            You can't set a DateTime to null. :)

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            S Offline
            S Offline
            Slacker007
            wrote on last edited by
            #8

            c# - How to set DateTime to null - Stack Overflow[^]

            realJSOPR 1 Reply Last reply
            0
            • S Slacker007

              I would not use Convert.ToDateTime with a text/string input. Instead try using TryParse with datetime. DateTime.TryParse Method (String, DateTime) (System)[^]

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

              I don't think you would set a DateTime to string.empty.

              S 1 Reply Last reply
              0
              • L Lost User

                I don't think you would set a DateTime to string.empty.

                S Offline
                S Offline
                Slacker007
                wrote on last edited by
                #10

                Of course you can't. But I don't know how they are working with the actual data and displaying it. I am starting to regret posting an answer to his question. :sigh:

                realJSOPR 1 Reply Last reply
                0
                • S Slacker007

                  c# - How to set DateTime to null - Stack Overflow[^]

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #11

                  That's not what I would call a reasonable practice. I prefer my DateTimes to be valid in some way (and use new DateTime(0) to accomplish that).

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                  S 1 Reply Last reply
                  0
                  • S Slacker007

                    Of course you can't. But I don't know how they are working with the actual data and displaying it. I am starting to regret posting an answer to his question. :sigh:

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #12

                    Slacker007 wrote:

                    I am starting to regret posting an answer to his question. :sigh:

                    Waaa-waaaa-waaaaaaaaaaa. :)

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                    S 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      That's not what I would call a reasonable practice. I prefer my DateTimes to be valid in some way (and use new DateTime(0) to accomplish that).

                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                      -----
                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                      -----
                      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                      S Offline
                      S Offline
                      Slacker007
                      wrote on last edited by
                      #13

                      John Simmons / outlaw programmer wrote:

                      That's not what I would call a reasonable practice.

                      It is a possible and valid scenario, whether you John, agree with it or not. Just saying... For instance, shipping dates - they are usually null until something actually ships.

                      realJSOPR L 2 Replies Last reply
                      0
                      • realJSOPR realJSOP

                        Slacker007 wrote:

                        I am starting to regret posting an answer to his question. :sigh:

                        Waaa-waaaa-waaaaaaaaaaa. :)

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        S Offline
                        S Offline
                        Slacker007
                        wrote on last edited by
                        #14

                        You know John, you don't "always" have to be an asshole.

                        realJSOPR 1 Reply Last reply
                        0
                        • S Slacker007

                          You know John, you don't "always" have to be an asshole.

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #15

                          Yes. Yes, I do. If I wasn't always an assh*le, I would disappoint my legions of fans. Besides, me being an assh*le is really the only constant in life any more that absolutely EVERYBODY can count on. Beyond that, I was just poking you in the ribs. If you're honestly butt-hurt, I can call a waaa-mbulance for you. :) BTW, I'm the one that voted your original answer a 5.

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                          1 Reply Last reply
                          0
                          • S Slacker007

                            John Simmons / outlaw programmer wrote:

                            That's not what I would call a reasonable practice.

                            It is a possible and valid scenario, whether you John, agree with it or not. Just saying... For instance, shipping dates - they are usually null until something actually ships.

                            realJSOPR Offline
                            realJSOPR Offline
                            realJSOP
                            wrote on last edited by
                            #16

                            We can argue the validity of nulls all day. I don't like 'em, and I strive to resolve them in some way.

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            1 Reply Last reply
                            0
                            • S Slacker007

                              John Simmons / outlaw programmer wrote:

                              That's not what I would call a reasonable practice.

                              It is a possible and valid scenario, whether you John, agree with it or not. Just saying... For instance, shipping dates - they are usually null until something actually ships.

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

                              One would use a "shipped event", in that case. One could drop and smash the shipment .... One does not carry a null "damaged date" in anticipation.

                              1 Reply Last reply
                              0
                              • A abdul subhan mohammed

                                sorry for that, plz help me.

                                D Offline
                                D Offline
                                deepankarbhatnagar
                                wrote on last edited by
                                #18

                                This is not the correct place to write this content.

                                hi

                                1 Reply Last reply
                                0
                                • S Slacker007

                                  I would not use Convert.ToDateTime with a text/string input. Instead try using TryParse with datetime. DateTime.TryParse Method (String, DateTime) (System)[^]

                                  C Offline
                                  C Offline
                                  ch nagaraju
                                  wrote on last edited by
                                  #19

                                  HOW TO RECTIFY ERROR

                                  S realJSOPR 2 Replies Last reply
                                  0
                                  • C ch nagaraju

                                    HOW TO RECTIFY ERROR

                                    S Offline
                                    S Offline
                                    Slacker007
                                    wrote on last edited by
                                    #20

                                    Why are you yelling at me?

                                    1 Reply Last reply
                                    0
                                    • C ch nagaraju

                                      HOW TO RECTIFY ERROR

                                      realJSOPR Offline
                                      realJSOPR Offline
                                      realJSOP
                                      wrote on last edited by
                                      #21

                                      Read the whole thread - the answer is there.

                                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                      -----
                                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                      -----
                                      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                      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