ExecuteNonQuery(DBCommand) returns -1 inappropriately.

Topics: Data Access Application Block
Mar 19, 2007 at 6:50 PM
I am using the Enterprise Library to insert a row into a database. When I call ExecuteNonQuery on the database, the row is inserted as expected but the return value is not the expected value of 1. Instead it is -1. In another similar method used for updating existing objects, the call to ExecuteNonQuery does return 1 as expected. Any ideas why I would get -1 back from a successful insert?

Here's the code:

// In my using section at the top of the file:
using DAAB = Microsoft.Practices.EnterpriseLibrary.Data;


// Code from my insert method:
DAAB.Database db = DAAB.DatabaseFactory.CreateDatabase(connectionStringName);
System.Data.Common.DbCommand dbc = db.GetStoredProcCommand("dbo.InsertChargeYieldByDetailId");

db.AddInParameter(dbc, "@DetailId", DbType.Int32, productDetailId);

db.AddInParameter(dbc, "@ActualCyComments", DbType.AnsiString, chargeYield.ActualCyComments);
db.AddInParameter(dbc, "@ActualCyVolume", DbType.Decimal, chargeYield.ActualVolume);
db.AddInParameter(dbc, "@PlannedBarrelsPerDay", DbType.Decimal, chargeYield.PlannedBPD);
db.AddInParameter(dbc, "@PlannedCyComments", DbType.AnsiString, chargeYield.PlannedCyComments);
db.AddInParameter(dbc, "@PlannedCyDollars", DbType.Decimal, chargeYield.PlannedDollars);
db.AddInParameter(dbc, "@PlanRate", DbType.Decimal, chargeYield.PlanRate);
db.AddOutParameter(dbc, "@ChargeYieldId", DbType.Int32, 4);

int rowCount = db.ExecuteNonQuery(dbc);
bool success = rowCount == 1;

if (success)
{
chargeYield.Id = (int)dbc.Parameters"@ChargeYieldId".Value;
}

return success;
Mar 19, 2007 at 9:02 PM
You may have

SET NOCOUNT ON

in your stored procedure.

Regards,

Dave

___________________

David Hayden
Microsoft MVP C#
Mar 19, 2007 at 9:11 PM

DavidHayden wrote:
You may have

SET NOCOUNT ON

in your stored procedure.


You are correct. NOCOUNT is sort of the evil nemesis of ADO, isn't it? Can't live without it in SELECT queries; can't live with it in UPDATE or INSERT queries.

Thanks for the help.

Dale