Capturing Stored Procedure return value

M

Mike P

I have a SQL Server stored procedure that is returning a 0 if the
execution was OK and 1 if not.

e.g. :

commit trans
return 0

on_error:
rollback tran
return 1

I am trying to capture this return value in my C# :

objConnection.Open();
int intResult = objCommand.ExecuteNonQuery();
objConnection.Close();


What am I doing wrong?


Any assistance would be really appreciated.


Cheers,

Mike
 
M

Matthias S.

Hi,

ExecuteNonQuery() will return you the rows affected if you do an update,
delete or insert.

you can use the ExecuteScalar() instead which returns the value of the
first colum and field.

hope that helps,

matthias
 
N

Nicholas Paldino [.NET/C# MVP]

Matthias and Mike,

This isn't really correct. If you use ExecuteScalar, and the stored
procedure returns a result set, then you will not get the result that you
want.

Rather, what you should do is create a parameterized command (which I
assume you have already) and attach a parameter where the Direction property
is ParameterDirection.ReturnValue. This parameter will then get the return
value result.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top