2

I have a stored procedure that client provided me

Like:

ALTER Proc [dbo].[XYZ]
    @Parameter varchar(100),
    @Parameter1 nvarchar(4000) out

   SET @APIString = "Test Test"

I have no rights to change this procedure.

When I execute procedure through C# I get a blank string from procedure

How to get the @Parameter1 value in my project?

C# Code:

SqlCommand cmd = new SqlCommand("dbo.XYZ", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Parameter", Parameter);

cmd.Parameters.Add("@Parameter1", SqlDbType.VarChar,4000);
cmd.Parameters["@Parameter1"].Direction = ParameterDirection.Output;

conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    String = reader["@Parameter1"] != null ? reader["@Parameter1"].ToString() : "";
}

conn.Close();
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56

1 Answers1

7

@Parameter1 is an output parameter. You can get its value the same way you set the values for input parameters, e.g.

var cmd = new SqlCommand("dbo.XYZ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Parameter", inputValue);

// add the output parameter
cmd.Parameters.Add("@Parameter1", SqlDbType.NVarChar).Direction =
    ParameterDirection.Output;

cmd.ExecuteNonQuery();

string parameter1 = (string)cmd.Parameters["@Parameter1"].Value;

You should also use ExecuteNonQuery unless the store procedure returns values with a select statement.

Dirk
  • 10,668
  • 2
  • 35
  • 49