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.

Patrick Tucci
  • 1,824
  • 1
  • 16
  • 22

2 Answers2

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;
coder231
  • 453
  • 2
  • 14
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#

Community
  • 1
  • 1
maniak1982
  • 707
  • 2
  • 7
  • 23