Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Getting THE" returned value of a stored procedure in ASP.net

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-02-27 : 09:28:32
Raymond writes "Given that I use IDbCommand dbCommand = new SqlCommand("storedProcName") ... and dbCommand.ExecuteNonQuery(); to run the stored procedure and it ends with the statement: RETURN 27 (for example)...

There appears to be no possible way to get the returned value of 27 from the dbCommand .. True or false ? Our application has a number of existing stored procedures that indicate an error by using "RETURN" statements as opposed to "OUTPUT" parameters.. I'd like to avoid hacking over someone else's stored procedure code and numerous application sources to create "OUTPUT" parameters.."

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-27 : 12:43:42
you need to add a param to your SqlCommand.Parameters collection to catch the return value. specify ParameterDirection.ReturnValue.

See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinputoutputparametersreturnvalues.asp


www.elsasoft.org
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-02-27 : 14:00:29
I haven't tested it, but after some googling I found this:

http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=809206&SiteID=17

From the last post on that page (again, not tested):

quote:

You must add a SqlParameter like this to your command object:

SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);

returnValue.Direction = ParameterDirection.ReturnValue;

After executing the command with e.g. ExecuteNonQuery(); you can get the return value of the stored procedure with

int returnV = (int)returnValue.Value;

Best regards




- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -