none
reader.hasrows is false RRS feed

  • Question

  • I have a SQLEXPRESS v15 table (called prmtcnfg) in a database called PLV. I am using VS 2019 and C#.

    The table has one row in it.

    I can connect to the server and it shows the table (and fields) in the Server Explorer Windows

    This is what I have so far... but the reader = command.ExecuteReader(); doesn't return the row.

    What am I missing?

    SqlConnection conn = new SqlConnection();           
    conn.ConnectionString = @"Data Source=WIN10\SQLEXPRESS;";
    conn.ConnectionString = conn.ConnectionString + "Initial Catalog=PLV;";
    conn.ConnectionString = conn.ConnectionString + "User id=PLV;";
    conn.ConnectionString = conn.ConnectionString + "Password=PLV;";
                
    conn.Open();
    MessageBox.Show("connection open"); // connects just fine

    // now go look for record

    SqlCommand command;
    SqlDataReader reader;

    // select all columns in the row

    string sql;
    sql = "Select Top 1 * from prmtcnfg";

    // this doesn't work either

    //sql = "Select * from prmtcnfg";

    command = new SqlCommand(sql, conn);            
    reader = command.ExecuteReader();

    // READER.HASROWS shows FALSE -- WHY??

            {
                if (reader.HasRows)
                  {
                    app.serinumb = Convert.ToString(reader["SeriNumb"]);
                    MessageBox.Show(app.serinumb);
                  }
                else
                  {
                    MessageBox.Show("no rows - has rows is false");
                  }

            }

    Wednesday, January 8, 2020 6:15 PM

Answers

  • Don't use if (reader.HasRows). Instead, you need if (reader.Read())

    The Read method is the one that brings in the first row and lets you access the content of the row. It returns false if there are no rows to read.

    • Marked as answer by J Vallee Wednesday, January 8, 2020 8:02 PM
    Wednesday, January 8, 2020 7:13 PM
    Moderator

All replies

  • Don't use if (reader.HasRows). Instead, you need if (reader.Read())

    The Read method is the one that brings in the first row and lets you access the content of the row. It returns false if there are no rows to read.

    • Marked as answer by J Vallee Wednesday, January 8, 2020 8:02 PM
    Wednesday, January 8, 2020 7:13 PM
    Moderator
  • Don't use if (reader.HasRows). Instead, you need if (reader.Read())

    The Read method is the one that brings in the first row and lets you access the content of the row. It returns false if there are no rows to read.

    Changing it to reader.Read() returns false also

    properties of reader say:

    isClosed false

    fieldcount 222

    Wednesday, January 8, 2020 7:23 PM
  • Just use a while(reader) loop and forget about HasRows()

    If there is a record, then it will be read in the loop.

    Wednesday, January 8, 2020 7:57 PM
  • Don't use if (reader.HasRows). Instead, you need if (reader.Read())

    The Read method is the one that brings in the first row and lets you access the content of the row. It returns false if there are no rows to read.

    Changing it to reader.Read() returns false also

    properties of reader say:

    isClosed false

    fieldcount 222

    This did work after all. Apparently the import into that table failed. The table is actually empty GRRR!!!


    Wednesday, January 8, 2020 8:03 PM