Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteNonQuery() returns -1 in Update when records are updated

I've verified that my method/Oracle procedure is working correctly, but in my C# I always get -1 returned from ExecuteNonQuery(). Consequently, the below bool is always false. We only use Triggers in our DB (Oracle) for INSERT statements. Do I need a trigger for an update statement?

Any suggestions why that would happen? Its definitely updating one record:

public bool ChangePassword(long UserId, string NewPassword)
    {
        int rcds = 0;
        using (OracleConnection dbConn = new OracleConnection(dbConnString))
        using (OracleCommand dbCmd = new OracleCommand("PKG_USER.CHANGE_PASSWORD", dbConn))
        {
            try
            {
                string salt = GenerateSalt();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add("p_USER_ID", OracleDbType.Int64, UserId, ParameterDirection.Input);
                dbCmd.Parameters.Add("P_PASSWORD", OracleDbType.Varchar2, 128, EncodePassword(NewPassword, this.IsPasswordHashed, salt), ParameterDirection.Input);
                dbCmd.Parameters.Add("P_PASSWORD_SALT", OracleDbType.Varchar2, 128, salt, ParameterDirection.Input);

                if (dbConn.State != ConnectionState.Open) dbConn.Open();
                rcds = dbCmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                LastError = e.Message + " " + e.Source;
                rcds = 0;
            }
            finally
            {
                dbCmd.Dispose();
                dbConn.Dispose();
            }
        }
        return (rcds > 0);
    }

Sorry... heres the SP:

PROCEDURE Change_Password(p_User_Id       IN Users.User_Id%TYPE,
                          p_Password      IN Users.Password%TYPE,
                          p_Password_Salt IN Users.Password_Salt%TYPE) IS


BEGIN
UPDATE Users
   SET Password             = p_Password,
       Password_Salt        = p_Password_Salt,
       Password_Change_Date = SYSDATE
 WHERE User_Id = p_User_Id;

 END Change_Password;
like image 521
Barryman9000 Avatar asked Feb 04 '10 20:02

Barryman9000


People also ask

What does ExecuteNonQuery () method return?

Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

Which of the following is the use of ExecuteNonQuery ()?

What is Execute Non Query? ExecuteNonQuery used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.

Can we use ExecuteNonQuery select statement?

This returns a SqlDataReader , which has a HasRows property. ExecuteNonQuery shouldn't be used for SELECT statements.

What is the use of ExecuteScalar () function in Ado net?

ExecuteScalar method is used to execute SQL Commands or storeprocedure, after executing return a single value from the database. It also returns the first column of the first row in the result set from a database.


2 Answers

Try explicitly returning SQL%ROWCOUNT.

According to MSDN, DbCommand..ExecuteNonQuery will always return -1 for stored procedure calls:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

If I remember correctly from my days of using lots of stored procs, I believe you would need to use an output argument to return stuff like the number of updated rows.

like image 125
MikeWyatt Avatar answered Sep 20 '22 11:09

MikeWyatt


I'm not an Oracle guy, but apparently there's a command:

set feedback off

which prevents it from returning the count of records affected. Is this line in the stored procedure? Or have you tried 'set feedback on'? Functionally I think this is just the reverse of SQL Server's SET NOCOUNT ON/OFF command.

like image 42
Steve Wortham Avatar answered Sep 19 '22 11:09

Steve Wortham