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. C#
  4. Specified Cast is not Valid - driving me nuts!

Specified Cast is not Valid - driving me nuts!

Scheduled Pinned Locked Moved C#
databasequestioncsharpvisual-studiocom
4 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.
  • J Offline
    J Offline
    Jasmine2501
    wrote on last edited by
    #1

    Why am I getting this error? My stored procedure is returning the value of an int column... try { SqlDataReader r = run_sql_reader(sql); if (r.Read()) { LB_CO_record_number.Text = (r.GetInt32(0)).ToString(); //***exception here } r.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } Visual Studio is not telling me what is trying to be casted to what else, so I can't figure out which thing is causing the error. I do this kind of thing all the time and I've never gotten this error. "Quality Software since 1983!"http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

    J 1 Reply Last reply
    0
    • J Jasmine2501

      Why am I getting this error? My stored procedure is returning the value of an int column... try { SqlDataReader r = run_sql_reader(sql); if (r.Read()) { LB_CO_record_number.Text = (r.GetInt32(0)).ToString(); //***exception here } r.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } Visual Studio is not telling me what is trying to be casted to what else, so I can't figure out which thing is causing the error. I do this kind of thing all the time and I've never gotten this error. "Quality Software since 1983!"http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

      J Offline
      J Offline
      Jasmine2501
      wrote on last edited by
      #2

      Mmmmkay... I figured it out. My proc was for some reason not returning an int column. I don't know what it was returning, but using "convert" on the value before sending it back fixed the problem. Crazy computers :)

      "Quality Software since 1983!"
      http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

      V 1 Reply Last reply
      0
      • J Jasmine2501

        Mmmmkay... I figured it out. My proc was for some reason not returning an int column. I don't know what it was returning, but using "convert" on the value before sending it back fixed the problem. Crazy computers :)

        "Quality Software since 1983!"
        http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

        V Offline
        V Offline
        Vasudevan Deepak Kumar
        wrote on last edited by
        #3

        By any chance did you get a NULL returned from the database. Attempting to convert NULL to another might succumb with System.InvalidCastException: The specified cast is invalid.

        Vasudevan Deepak Kumar Personal Homepage Tech Gossips
        Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

        J 1 Reply Last reply
        0
        • V Vasudevan Deepak Kumar

          By any chance did you get a NULL returned from the database. Attempting to convert NULL to another might succumb with System.InvalidCastException: The specified cast is invalid.

          Vasudevan Deepak Kumar Personal Homepage Tech Gossips
          Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

          J Offline
          J Offline
          Jasmine2501
          wrote on last edited by
          #4

          No it returns a number, which I verified, but it's good to know that. I'll add some code to check for that. Thanks!

          "Quality Software since 1983!"
          http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

          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