Good afternoon, I can not understand why I can not add the second value to the parameters of the procedure, it gives an error

The SqlParameter is a SqlParameterCollection

ValueSQLDataPC.FlagBegin = true; ResultPCInfo = GetMethod.GetPCData(ID, IP); ValueDataConnect.SendCommand.CommandText = "sp_CheckIndex"; ValueDataConnect.SendCommand.CommandType = CommandType.StoredProcedure; SqlParameter[] ParameterArray = new SqlParameter[2]; ValueDataConnect.SendCommand.Parameters.Clear(); ValueDataConnect.SendCommand.Parameters.Add("@CheckCode", SqlDbType.Int, 342); SqlParameter ReturnResult = new SqlParameter("@ResultSearch", SqlDbType.VarChar); ReturnResult.Value = ""; ReturnResult.Direction = ParameterDirection.ReturnValue; ValueDataConnect.SendCommand.Parameters.Add(ReturnResult); ValueDataConnect.SendCommand.ExecuteNonQuery(); ResultCheck = Convert.ToString(ValueDataConnect.SendCommand.Parameters["@ResultSearch"].Value); 

    2 answers 2

    Each SqlParameter object can only be used once. In your code, there is no reuse of the same SqlParameter , so use the debugger to find a specific line. Most likely it will be a string like

     someCommand.Parameters.Add(someParameter); 

    where the same object someParameter was previously transferred to another call of type someOtherCommand.Parameters.Add(someParameter) . Remove the reuse of this someParameter , and the error disappears.

    • Thanks for the answer, but Microsoft did not see in the articles that this parameter is used once and many examples show a similar construction, and how then you can use a set of variables with parameters for the procedure, tried the same array - SergD29
    • one
      never seen reuse parameter in msdn. link please? maybe I did not quite unequivocally put it - you have a problem not in the code that you gave in the question Can you specify a specific line on which it falls? - PashaPash
    • I mean in the description, an example of articles ( stackoverflow.com/questions/11044214/… ) line with an error ValueDataConnect.SendCommand.Parameters.Add (ReturnResult); - SergD29 pm
    • one
      in the article by reference, each parameter is used once. did you accurately bring all the code because in the form in which it is in your question, it should not fall. There may be a problem in the reuse of SendCommand - SendCommand -create it - or in the implementation features of ValueDataConnect . - PashaPash
    • one
      You did not understand a bit - you can add the same parameter to different collections - for example, to different arrays of the type SqlParameter[] . But after adding it to the collection of exactly the SqlParameterCollection type SqlParameterCollection it binds to it, and trying to add it to another SqlParameterCollection it throws an error. - PashaPash

    4 hours spent on it

    ParameterArray [1] .Direction = ParameterDirection.Output;

    thank you PashaPash