ExecuteScalar() not returning single field value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmo63126@yahoo.com

    #1

    ExecuteScalar() not returning single field value

    Why am I not able to retrieve the value of ID (which does exist) using
    ExecuteScalar() ? Do i simply have to use ExecuteReader() instead?

    My problem snippet:

    string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
    //TableID is a unique ID within Table, no dupiclates-risk

    myCommand = new SqlCommand(mySQ L, myConn);
    myConn.Open();
    int myID = (int)myCommand. ExecuteScalar() ;
    myConn.Close();
    return myID;

    My result:
    Object reference not set to an instance of an object.

  • Andrew Robinson

    #2
    Re: ExecuteScalar() not returning single field value

    Is there a record with TableID=1 ?

    Is the error being generated on the ExecuteScalar() invocation or a
    different line?

    You might try object obj = myCommand.Execu teScalar();
    and inspect the value of the object or convert it if it is not null.

    -Andrew


    <cmo63126@yahoo .com> wrote in message
    news:1113494085 .036209.123690@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > Why am I not able to retrieve the value of ID (which does exist) using
    > ExecuteScalar() ? Do i simply have to use ExecuteReader() instead?
    >
    > My problem snippet:
    >
    > string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
    > //TableID is a unique ID within Table, no dupiclates-risk
    >
    > myCommand = new SqlCommand(mySQ L, myConn);
    > myConn.Open();
    > int myID = (int)myCommand. ExecuteScalar() ;
    > myConn.Close();
    > return myID;
    >
    > My result:
    > Object reference not set to an instance of an object.
    >[/color]


    Comment

    • Marina

      #3
      Re: ExecuteScalar() not returning single field value

      It's not executing the query that is doing it.

      You should have the line number that this is failing on: so which line is
      it?

      <cmo63126@yahoo .com> wrote in message
      news:1113494085 .036209.123690@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > Why am I not able to retrieve the value of ID (which does exist) using
      > ExecuteScalar() ? Do i simply have to use ExecuteReader() instead?
      >
      > My problem snippet:
      >
      > string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
      > //TableID is a unique ID within Table, no dupiclates-risk
      >
      > myCommand = new SqlCommand(mySQ L, myConn);
      > myConn.Open();
      > int myID = (int)myCommand. ExecuteScalar() ;
      > myConn.Close();
      > return myID;
      >
      > My result:
      > Object reference not set to an instance of an object.
      >[/color]


      Comment

      • cmo63126@yahoo.com

        #4
        Re: ExecuteScalar() not returning single field value

        me = dumbA$$;

        the variable I was passing in, unrelated to the simpler example posted
        here, was the problem.

        my apologies. :(.

        Comment

        Working...