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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Assigning the result of an SQL ExecuteScalar() to an int?

Assigning the result of an SQL ExecuteScalar() to an int?

Scheduled Pinned Locked Moved C#
databasequestion
5 Posts 4 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.
  • R Offline
    R Offline
    Red_Wizard_Shot_The_Food
    wrote on last edited by
    #1

    I want to assign the results of an SQL ExecuteScalar to an int. Which is fine when the ExecuteScalar returns a value (i.e the paramater sent to the stored procedure caused the SELECT function to find a valid record). BUT if the paramater does not pass the SELECT procedure a value that is already in the table the assignment to an int causes an exception (as no record was returned). Can i do this without using a try/catch block? Thanks.

    P 1 Reply Last reply
    0
    • R Red_Wizard_Shot_The_Food

      I want to assign the results of an SQL ExecuteScalar to an int. Which is fine when the ExecuteScalar returns a value (i.e the paramater sent to the stored procedure caused the SELECT function to find a valid record). BUT if the paramater does not pass the SELECT procedure a value that is already in the table the assignment to an int causes an exception (as no record was returned). Can i do this without using a try/catch block? Thanks.

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

      Red_Wizard_Shot_The_Food wrote:

      I want to assign the results of an SQL ExecuteScalar to an int. Which is fine when the ExecuteScalar returns a value (i.e the paramater sent to the stored procedure caused the SELECT function to find a valid record). BUT if the paramater does not pass the SELECT procedure a value that is already in the table the assignment to an int causes an exception (as no record was returned). Can i do this without using a try/catch block?

      You could return it as an object and then cast it if the object was not null.

      the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
      Deja View - the feeling that you've seen this post before.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Red_Wizard_Shot_The_Food wrote:

        I want to assign the results of an SQL ExecuteScalar to an int. Which is fine when the ExecuteScalar returns a value (i.e the paramater sent to the stored procedure caused the SELECT function to find a valid record). BUT if the paramater does not pass the SELECT procedure a value that is already in the table the assignment to an int causes an exception (as no record was returned). Can i do this without using a try/catch block?

        You could return it as an object and then cast it if the object was not null.

        the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
        Deja View - the feeling that you've seen this post before.

        A Offline
        A Offline
        aSarafian
        wrote on last edited by
        #3

        As far as i know you can't do it without using try catch or an if statement which in this case is the same. Convertion in .Net assumes that the converted value is not null. So any testing should be done prior assuming your id in base are not zero-valued i have implemented a function that executed the command and return zero if not found or the desired value. I have implemented something similar for strings also.

        C 1 Reply Last reply
        0
        • A aSarafian

          As far as i know you can't do it without using try catch or an if statement which in this case is the same. Convertion in .Net assumes that the converted value is not null. So any testing should be done prior assuming your id in base are not zero-valued i have implemented a function that executed the command and return zero if not found or the desired value. I have implemented something similar for strings also.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Sarafian wrote:

          you can't do it without using try catch or an if statement which in this case is the same.

          No it isn't. A try/catch adds a heck of a processing overhead if an exception is raised. Since this is not an exceptional case getting the value as an object then testing for null would be the better option.

          Sarafian wrote:

          assuming your id in base are not zero-valued i have implemented a function that executed the command and return zero if not found or the desired value.

          Returning magic numbers is not the solution. Testing for null or DBNull.Value is. It should also be realised that null is not the same as DBNull.Value. null is returned if the SELECT statement returned nothing at all. DBNull.Value is returned if the SELECT statement found the row and column, but the column was null.


          Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          A 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Sarafian wrote:

            you can't do it without using try catch or an if statement which in this case is the same.

            No it isn't. A try/catch adds a heck of a processing overhead if an exception is raised. Since this is not an exceptional case getting the value as an object then testing for null would be the better option.

            Sarafian wrote:

            assuming your id in base are not zero-valued i have implemented a function that executed the command and return zero if not found or the desired value.

            Returning magic numbers is not the solution. Testing for null or DBNull.Value is. It should also be realised that null is not the same as DBNull.Value. null is returned if the SELECT statement returned nothing at all. DBNull.Value is returned if the SELECT statement found the row and column, but the column was null.


            Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            A Offline
            A Offline
            aSarafian
            wrote on last edited by
            #5

            Thanks. Didn't know the difference.

            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