Developer forum

Forum » Development » Using ExecuteNonQuery with a Stored Procedure

Using ExecuteNonQuery with a Stored Procedure

Lars Bo Wassini Dynamicweb Employee
Lars Bo Wassini
Reply

I'm trying to create a Scheduled Task AddIn based on a Stored Procedure, but whatever I return from my stored procedure, ExecuteNonQuery always returns -1

This means that the sceduled task always show as Error.

What could be wrong?

public class BuildDiscountsScheduledTask : BaseScheduledTaskAddIn
{
public override bool Run()
{
ILogger logger = LogManager.Current.GetLogger("BuildExpandedDiscountTable");
 
var query = new CommandBuilder();
query.Add("EXEC [BuildExpandedPriceDiscTable]");
 
int result = Database.ExecuteNonQuery(query, 720);
return result >= 0;
}
}
 
Stored Procedure:
 
ALTER     PROCEDURE [dbo].[BuildExpandedPriceDiscTable] AS
RETURN 1;
 
 

Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Have you tried to use Database.ExecuteScalar instead?

/Morten

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

I am pretty sure that ExecuteNonQuery always returns the number of affected rows or -1 for non-DML statements, and does not give you the return value. So, with a procedure like 

SET NOCOUNT OFF
DELETE FROM SomeTable where ID < 10

you will get the number of rows affected by the delete.

In your case, you have a return statement, and not affected rows. You can do what Morten says by doing a select

SELECT 1

and use ExecuteScalar or set up a command to handle return values from the procedure.

Imar

Votes for this answer: 1

 

You must be logged in to post in the forum