views:

241

answers:

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>
          }
       }
+1  A: 

Try:

if (myReader.HasRows)
  while (myReader.Read())
  .....
pierre
+1 - how I'd do it.
Antony Koch
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.
TampaRich