title: Error: "The SqlParameter is already contained by another SqlParameterCollection" when adding a Parameter
body: <p>I have a few similar functions that interact with my SQL server (selects, calls to stored procdures etc.) and all work with the exception of the one below.  Each <code>SqlConnection</code> is contained within a using block with the <code>SqlCommand</code> also contained within a using block.</p>
  
  <p>This code is failing when attempting to add the <code>@LastUpdated</code> parameter. I've tried some suggestions I've seen in other posts: <code>cmd.Parameters.Clear()</code>, wrapping in using, etc., but no luck. A few posts with the same error were resolved when duplicate attempts to set the same parameter where found. It's possible I'm missing that but I've looked over this for a few hours, even cleaned the glasses. Any direction would be appreciated.</p>
  
  <pre><code>private void _AddCartItem(bool hasEventInCart, _EventCart cartContents, ref int cartItemId)
  {
      using (SqlConnection sqlConnection = new SqlConnection(_striMISConnection))
      {
          sqlConnection.Open();
  
          using (SqlCommand cmd = sqlConnection.CreateCommand())
          {
              SqlParameter param = new SqlParameter();
              cmd.Parameters.Clear();
  
              // add/update CartItem
              if (hasEventInCart)
              {
                  // Update item
                  cmd.CommandText = "SProc2Insert @CartItemId, @ID, 'Event', @Created, @LastUpdated";
                  param.ParameterName = "@CartItemId";
                  param.Value = cartItemId;
                  cmd.Parameters.Add(param);
              }
              else
              {
                  // add item
                  cmd.CommandText = "SProc2Update @ID, 'Event', @Created, @LastUpdated";
              }
  
              cmd.CommandType = CommandType.Text;
  
              param.ParameterName = "@Created";
              param.Value = DateTime.Now.ToString();
              cmd.Parameters.Add(param);
  
              param.ParameterName = "@LastUpdated";
              param.Value = DateTime.Now.ToString();
              **cmd.Parameters.Add(param);**
  
              param.ParameterName = "@ID";
              param.Value = this.ID;
              cmd.Parameters.Add(param);
  
              if (hasEventInCart)
              {
                  cmd.ExecuteNonQuery(); // do the update
              }
              else
              {
                  cartItemId = (int)cmd.ExecuteScalar();
  
                  foreach (var currentCartEvent in cartContents.CartEvents)
                  {
                      if (currentCartEvent.EventCode == this.EventCode)
                      {
                          currentCartEvent.CartItemID = cartItemId;
                      }
                  }
              }
              cmd.Parameters.Clear();
          }
      }
  }
  </code></pre>