1

I have a basic sql statement that looks up a user and returns one record but when I run a block of code that says if(myReader.Read()) it returns false. I have stepped through the code and examined the reader object and it does in fact contain one record. below is the code.

sql: SELECT user_name, user_password, user_state FROM users WHERE users.user_id = 123

    System.Data.Common.DbCommand _cmd = this.GetCommand(conn, _dbf, sqlText, CommandType.Text);
     System.Data.Common.DbConnection _cn = _cmd.Connection;
     System.Data.Common.DbDataReader myReader = null;

     _cn.Open();
     using(_cn) {
        myReader = _cmd.ExecuteReader();
        if (myReader.Read())  {
                <object gets built here with user data returned from sql>
          }
       }
2
  • 1
    Does this query return results when run from sqlplus or sql developer? Commented Feb 4, 2010 at 13:16
  • yes, ran the sql in toad and it returned results. Verified the connection string was pointing to the correct database. Commented Feb 4, 2010 at 14:38

1 Answer 1

1

Try:

if (myReader.HasRows)
  while (myReader.Read())
  .....
Sign up to request clarification or add additional context in comments.

1 Comment

that is how I would have done it but it is not my codebase to change. Just researching why another developers code is not working. Probably have them change it to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.