c#: ExecuteNonQuery() returns -1

Funky picture Funky · Nov 14, 2011 · Viewed 41.4k times · Source

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 the 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?

Answer

kol picture kol · Nov 14, 2011

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

--source