0

The sql statement "Select Count(Marked) from Results Where Marked = true" is supposed to return the count of items marked. But I am getting 0 where there should be ten. I can see the ten marked items. I tried several forms of the sql statement. I get no errors, just 0. This is an Access database.

        sql = "SELECT COUNT(*) FROM Results WHERE Marked = true";
        cmd = new OleDbCommand(sql, con);
        Int32 num = (Int32)cmd.ExecuteNonQuery();
        con.Close();
        return num != 0;

I cannot find an example using actual C# code so I am not sure the syntax is correct.

ScottinTexas
  • 171
  • 1
  • 19
  • 3
    ExecuteNonQuery is for executing "non query". It returns affected records and you don't change data so you get 0. You probably want to use ExecuteScalar instead. – Ralf Jun 01 '22 at 11:58
  • seems like a duplicate of: https://stackoverflow.com/questions/10354543/return-value-from-oledbcommand – fbede Jun 01 '22 at 12:01

1 Answers1

1

Try this

sql = "SELECT COUNT(*) FROM Results WHERE Marked = true";
cmd = new OleDbCommand(sql, con);
//Int32 num = (Int32)cmd.ExecuteNonQuery();
Int32 num = (Int32)cmd.ExecuteScalar();
con.Close();
return num != 0;