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. System.FormatException: String was not recognized as a valid DateTime

System.FormatException: String was not recognized as a valid DateTime

Scheduled Pinned Locked Moved ASP.NET
helptutorial
6 Posts 2 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 Offline
    A Offline
    Asif Rehman
    wrote on last edited by
    #1

    When I don't pass certain value in textbox. It gives me above exception. Here's the code.

    cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;

    I want to ignore NULL values. How to tackle this problem. I know it should have been easy. But anyhow, I stuck . Regards Asif Rehman

    P 1 Reply Last reply
    0
    • A Asif Rehman

      When I don't pass certain value in textbox. It gives me above exception. Here's the code.

      cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;

      I want to ignore NULL values. How to tackle this problem. I know it should have been easy. But anyhow, I stuck . Regards Asif Rehman

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If the value in txtDOB.Text is a null or whitespace character, you need to set Value to DBNull.Value.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        If the value in txtDOB.Text is a null or whitespace character, you need to set Value to DBNull.Value.

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        A Offline
        A Offline
        Asif Rehman
        wrote on last edited by
        #3

        thanks for quick response. So to my best understanding, I can replace old phrase

        cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;

        with this one

        cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = (string.IsNullOrEmpty( txtDOB.Text)) ? DBNull.Value.ToString() : txtDOB.Text;

        Is there any way shorter to do this. Regards Asif Rehman

        P 1 Reply Last reply
        0
        • A Asif Rehman

          thanks for quick response. So to my best understanding, I can replace old phrase

          cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;

          with this one

          cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = (string.IsNullOrEmpty( txtDOB.Text)) ? DBNull.Value.ToString() : txtDOB.Text;

          Is there any way shorter to do this. Regards Asif Rehman

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I normally use a utility method like this:

          public static object GetValueForParameter(string value)
          {
          if (!string.IsNullOrWhitespace(value))
          return value;
          return DBNull.Value;
          }

          Then you can replace your code with cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = Utility.GetValueForParameter(txtDOB.Text);

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            I normally use a utility method like this:

            public static object GetValueForParameter(string value)
            {
            if (!string.IsNullOrWhitespace(value))
            return value;
            return DBNull.Value;
            }

            Then you can replace your code with cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = Utility.GetValueForParameter(txtDOB.Text);

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            A Offline
            A Offline
            Asif Rehman
            wrote on last edited by
            #5

            Thanks. It works. Regards Asif Rehman

            P 1 Reply Last reply
            0
            • A Asif Rehman

              Thanks. It works. Regards Asif Rehman

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You're welcome. Thanks for letting me know.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              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