Question

Running thru examples of Enterprise Library 5.0 and when I use ExecuteNonQuery to run an update sproc, it returns 2. The update is based on ProductID, the table's Primary Key (yes, I checked, and it is unique).

Here is the simple sproc:

ALTER PROCEDURE [dbo].[UpdateProductsTable]
     @ProductID int, 
     @ProductName varchar(50) = NULL, 
     @CategoryID int = NULL, 
     @UnitPrice money = NULL
AS
BEGIN
    UPDATE Products
    SET 
        ProductName = @ProductName, 
        CategoryID = @CategoryID, 
        UnitPrice = @UnitPrice
    WHERE ProductID = @ProductID
END

Executing this sp in SSMS shows "1 rows" in bottom right hand corner of the query results window, and a return value of 0 in the grid. Clicking on the messages tab shows

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

not sure why I'm seeing this 3 times here, but I don't believe that is the issue.

Here is the code calling the sp:

public static void Exec_Update_Query()
        //updates a column of a single row, checks that the update succeeded, and then update it again to return it to the original value 
        {
            string oldName = "Chai";
            string newName = "Chai Tea";

            SqlDatabase defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>() as SqlDatabase;

            // Create command to execute the stored procedure and add the parameters.
            DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
            defaultDB.AddInParameter(cmd, "ProductID", DbType.Int32, 1);
            defaultDB.AddInParameter(cmd, "ProductName", DbType.String, newName);
            defaultDB.AddInParameter(cmd, "CategoryID", DbType.Int32, 1);
            defaultDB.AddInParameter(cmd, "UnitPrice", DbType.Currency, 18);

            // Execute the query and check if one row was updated.
            int i = defaultDB.ExecuteNonQuery(cmd);
            if (i == 1)
            {
              // Update succeeded.
            }
            else
            {
                Console.WriteLine("ERROR: Could not update just one row.");
            }

            // Change the value of the second parameter
            defaultDB.SetParameterValue(cmd, "ProductName", oldName);

            // Execute query and check if one row was updated
            if (defaultDB.ExecuteNonQuery(cmd) == 1)
            {
              // Update succeeded.
            }
            else
            {
                Console.WriteLine("ERROR: Could not update just one row.");
            }
        }

I'm using int i to view the return value from the method and it returns 2. Any ideas why this would be? This is Enterprise Libarary 5.0 in VS2010 running against SQL 2005. Pretty straightforward but perplexing.

Was it helpful?

Solution

If I recall correctly, the result of any triggers that fire as a result of your commands will also be included in the returned row count. Most likely, this is your issue.

EDIT: Documentation

From MSDN SqlCommand.ExecuteNonQuery:

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top