I have a class that wraps some Sql functionality up and am having an issue with a return parameter that I'm trying to use. I've read a lot of the posts on this site with similar topics, but am not finding a solution that works for me. I've tried to use the method outlined in this related topic, but I still seem to have an issue.
Here is a snippet of the stored procedure (I've removed most of it for brevity):
INSERT INTO A_Table (col1, col2, col3)
VALUES (val1, val2, val3)
DECLARE @id int
SET @id = SCOPE_IDENTITY()
RETURN @id
This is a snippet of the C#:
SqlParameter rtnParam = new SqlParameter("@id", SqlDbType.Int);
rtnParam.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(rtnParam);
IAsyncResult result = command.BeginExecuteNonQuery();
command.EndExecuteNonQuery(result);
The command.BeginExecuteNonQuery() throws the following exception:
IndexOutOfRangeException: An SqlParameter with ParameterName 'id' is not contained by this SqlParameterCollection.
Is there something wrong with the way that I am adding the return parameter? Let me know if you need to see more code or any clarification. Thanks!