0

So this should return @rez, but instead it returns @idCEL. Why?

int rez=0;
var cmd = new SqlCommand(@"exec spUpdateCELPowerMeasurementForGeoData1
                         @idCEL, @rez OUT", conn);
cmd.Parameters.AddWithValue("@idCEL",idCEL).Direction=System.Data.ParameterDirection.Input;
cmd.Parameters.AddWithValue("@rez", rez).Direction=System.Data.ParameterDirection.Output;
cmd.CommandTimeout=700;
cmd.ExecuteNonQuery();

rez = int.Parse(cmd.Parameters[@rez].Value.ToString());

return rez;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

9

Use quotes around @rez - it should be a string:

rez = int.Parse(cmd.Parameters["@rez"].Value.ToString());

@rez is actually the rez local variable, not the "rez" sql parameter. It is always zero, so you are getting the first parameter instead.

I'd also avoid using ToString and Parse. If your stored procedure is returning a number, you can simply do (int)cmd.Parameters["@rez"].Value, or Convert.ToInt32

Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292