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. ExecuteScalar return value

ExecuteScalar return value

Scheduled Pinned Locked Moved C#
databasesql-serversysadminquestion
6 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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

    G K L M P 5 Replies Last reply
    0
    • V vanikanc

      Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      Try;

      string name = sqlcommRights.ExecuteScalar().ToString();

      Though, Its a good idea to check the result isnt null before trying to access it.

      Regards, Gareth. (FKA gareth111)

      1 Reply Last reply
      0
      • V vanikanc

        Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        I'm surprised 2 & 3 don't work. The thing to do to figure out why they don't: 1. What is the error message you are getting? 2. In version 3, have you put a breakpoint on to see what value you get back - it is a good idea to check this in code even if it works on the SQL side.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        1 Reply Last reply
        0
        • V vanikanc

          Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

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

          What is the SQL code that you are calling? Is it a stored proc? If yes, did you put a SELECT statement there?

          1 Reply Last reply
          0
          • V vanikanc

            Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

            M Offline
            M Offline
            Matt Meyer
            wrote on last edited by
            #5

            If everything is working when you run the query manually (sounds like you have verified this already), use method #3 and check if name is null, and if not, check name.GetType() to see what the framework thinks the type being returned from the command is.

            1 Reply Last reply
            0
            • V vanikanc

              Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Those look reasonable, but check for System.DBNull.Value Here's my current code for ExecuteScalar:

                  public virtual T
                  ExecuteScalar<T>
                  (
                      T IfNull
                  )
                  {
                      lock ( this.command )
                      {
                          try
                          {
                              this.OpenCount++ ;
              
                              object result = this.command.ExecuteScalar() ;
              
                              if ( ( result == null ) || ( result == System.DBNull.Value ) ) 
                              {
                                  result = IfNull ;
                              }
                              else if ( typeof(T) != result.GetType() )
                              {
                                  result = System.Convert.ChangeType
                                  (
                                      result
                                  ,
                                      typeof(T)
                                  ) ;
                              }
                              this.RaiseEvent ( this.OnSuccess , 1 , null ) ;
                              return ( (T) result ) ;
                          }
                          catch ( System.Exception err )
                          {
                              throw ( this.WrapException
                              (
                                  "ExecuteScalar"
                              ,
                                  this.command
                              ,
                                  err
                              ) ) ;
                          }
                          finally
                          {
                              this.OpenCount-- ;
                          }
                      }
                  }
              
              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