Hi, I would like to ask how to pass "select count" query value into variable?

string workstr = "SELECT COUNT(Indicator)FROM PunchIn WHERE EmployeeName='" + selectedItem + "'AND Months='" + selectedItem1 + "'";
                
                   AccessDb_Cmd.ExecuteNonQuery(workstr, Access_Db);
                    
                  int count = (int)??.ExecuteScalar();//what should put into "??" ?

Edited 5 Years Ago by leo88: n/a

Since it is a query, why would you perform line 4 (ExecuteNonQuery)?

and it would be int count = (int)AccessDb_Cmd.ExecuteScalar(); but you'll have to set the command parameter of the AccessDb_Cmd first.

ExecuteNonQuery is a method inside my sql class, AccessDb_Cmd is a class name, coding inside the ExecuteNonQuery are:

class AccessDbCmd : AccessDB
    {
        private OleDbCommand OleDbCmd;

 public bool ExecuteNonQuery(String Cmd, AccessDB mTrans)
        {
            try
            {
                OleDbCmd = new OleDbCommand(Cmd, mTrans.TransConn, mTrans.Trans);
                if (OleDbCmd.ExecuteNonQuery() != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);               
            }
            finally
            {
                OleDbCmd.Dispose();
            }
        }

Because of this condition, I have no idea of how to access "OleDbCmd " in line 9 to do the "ExecuteScalar". Sorry for my poor explaination.

Edited 5 Years Ago by leo88: n/a

Since it is a query, why would you perform line 4 (ExecuteNonQuery)?

and it would be int count = (int)AccessDb_Cmd.ExecuteScalar(); but you'll have to set the command parameter of the AccessDb_Cmd first.

Problem had been solved, your suggestion had helped me, thank you:)

This question has already been answered. Start a new discussion instead.