Search code examples
c#executenonquery

Why does ExecuteNonQuery() always return -1?


Ive used this method before to return the amount of rows changed. I am it to run an insert method, the insert runs fine in the stored procedure, but the return value from ExecuteNonQuery() always returns -1.

Here is my C# code:

int ret = 0;

using (SqlConnection conn = new SqlConnection(this.ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand(QueryName, conn))
    {
        conn.Open();

        if (Params != null)
            cmd.Parameters.AddRange(Params);

        cmd.CommandType = CommandType.StoredProcedure;
        
        ret = cmd.ExecuteNonQuery();

        conn.Close();
    }
}

return ret;

Why do I get -1 instead of the actual number of rows changed?


Solution

  • From MSDN:

    If you use this method to call a store procedure that perform UPDATE/INSERT in a table the method return -1 if the stored procedure has the SET NOCOUNT at ON value.