tags:

views:

32

answers:

3
public bool Update()
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@EmpId", EmpId);
            if (string.IsNullOrEmpty(Name))
            {
                param[1] = new SqlParameter("@Name", DBNull.Value);
            }
            else
            {
                param[1] = new SqlParameter("@Name", Name);
            }
            if (Salary == null)
                param[2] = new SqlParameter("@Salary", DBNull.Value);
            else
                param[2] = new SqlParameter("@Salary", Salary);
            if (DateofBirth  == null)
                param[3] = new SqlParameter("@DateofBirth", DBNull.Value);
            else
                param[3] = new SqlParameter("@DateofBirth", DateofBirth);
            if (DeptId  == null)
                param[4] = new SqlParameter("@DeptId", DBNull.Value);
            else
                param[4] = new SqlParameter("@DeptId", DeptId);
            return (Convert.ToInt32(SqlHelper.ExecuteNonQuery(CONNECTION_STRING, "InsertEmployee", param)) > 0);             
        }

Even if stored procedure executes, returning false.Why

+3  A: 

ExecuteNonQuery returns the number of rows affected. Based off contextual clues it looks like the stored procedure didn't insert a row for some reason.

popester
A: 

SqlHelper.ExecuteNonQuery returns the number of rows affected by the command (e.g. for an INSERT or UPDATE).

This is not > 0 for a function or procedure, by definition - because the SQL system can't know how many rows the function or procedure might have affected.

ref

Jeffrey Kemp
A: 

Do you have a SET NOCOUNT ON at the beginning of your stored proc?

In that case, the stored procedures doesn't return the number of records that were affected by its operations, and ADO.NET's SqlCommand interprets that as a failure.

marc_s

related questions