locked
Call to C# query returns negative one (-1) RRS feed

  • Question

  • User-1682007629 posted

     

     my database table named "tblTempForEditor" has 6 records in it. yet when I execute the code below I am getting a negative value that is returned. Can someone look over my code below to see if they can determine what the problem is?

        protected int GetRecordCount()
        {
            int rowsFound = 0;
            string connString = ConfigurationManager.ConnectionStrings["EMRConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string strQuery = "select count(*) from tblTempForEditor";
            SqlCommand cmd = new SqlCommand(strQuery, conn);
            cmd.Connection.Open();
            rowsFound = cmd.ExecuteNonQuery();
            return rowsFound;
        }

    Wednesday, June 10, 2009 4:15 PM

Answers

  • User-60558687 posted

     ExecuteNonQuery is for things like update

    use 

    rowsFound = int.Parse(cmd.ExecuteScalar().ToString());

    instead.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2009 4:25 PM
  • User-1682007629 posted

    Super cool! thank you!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2009 4:30 PM

All replies

  • User-60558687 posted

     ExecuteNonQuery is for things like update

    use 

    rowsFound = int.Parse(cmd.ExecuteScalar().ToString());

    instead.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2009 4:25 PM
  • User-1682007629 posted

    Super cool! thank you!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2009 4:30 PM