0

I have a return code from a stored procedure that I need to verify. I can't change the proc because it is part of a vendor package. Changing it to an output parameter is not possible. I don't care so much about the parameters as capturing the return code in c#. Here is an example of what I'm trying to do: Procedure:

create proc tester
   @parm nvarchar(3)
as

if @parm = 'Yes'
       return 1
else
       return -1
go

C# sample code:

SqlConnection sqlMon = new SqlConnection("Whatever");

sqlMon.Open();
SqlCommand sqlcomm = new SqlCommand("tester", sqlMon);
SqlDataReader sqlR; 
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.Parameters.Add(new SqlParameter("@parm", "Yes"));

sqlR = sqlcomm.ExecuteReader();

sqlMon.Close();

What I can't figure out is how do I now get the return code. Any help you can provide is appreciated. Thanks.

0

2 Answers 2

2

You can add a new parameter as below.

    SqlParameter ret = sqlcomm.Parameters.Add("@R", SqlDbType.Int);
    ret.Direction = ParameterDirection.ReturnValue;
    string returnvalue = (string)sqlcomm.Parameters["@R"].Value;
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. I had to add the return code as the first parameter. Than you!
0

ExecuteReader() returns a SqlDataReader:

SqlDataReader dr = sqlComm.ExecuteReader();

You can then work with the SqlDataReader to bind the data to a control, or whatever else you'd like to do.

https://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx

Here is an example of how to bind a DataReader to a GridView:

Binding sqldatareader to gridview c#

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.