title: Parameter "@xxx" has already been defined
body: <p>I have the code below and when it enters into the ForEach loop, the error hapens.</p>
  
  <p>I read this</p>
  
  <p><a href="https://stackoverflow.com/questions/2925258/sql-error-c-sharp-parameter-already-defined">StackOverFlow</a></p>
  
  <p>But didnt made it =\ I need to know what do I have to change. I tryed to move the MySqlCommand, but didnt solve the problem... Thanks
  I had 3 different methods (Select,Insert,Update) But i had to use "Transaction" So I puted the 3 methods at the same... First time it works fine, but the second loop throught the error happens in the <strong>INSERT QUERY</strong></p>
  
  <pre><code>#region Querys
          string Select = @"SELECT id_convidado, nome, cod_dependente, dt_insercao, matricula FROM convidado_acesso WHERE debpag = 0 AND status &lt;&gt; 'Devolvido'";
  
          string Insert = @"INSERT INTO debpag (
                       numero_int,
                       parcela,
                       tipo_pessoa,
                       matricula,
                       cod_dependente,
                       ev_financeiro,
                       quantidade,
                       valor_unitario,
                       valor_total,
                       dt_lancamento,
                       dt_vencimento,
                       dt_pagamento,
                       dt_insercao,
                       dt_insercao_pagamento,
                       referencia,
                       cobrador_lancto,
                       cobrador_pagto,
                       usr_lancto,
                       forma_pagto)
                       VALUES (
                       @numero_int,
                       @parcela,
                       @tipo_pessoa,
                       @matricula,
                       @cod_dependente,
                       @ev_financeiro,
                       @quantidade,
                       @valor_unitario,
                       @valor_total,
                       @dt_lancamento,
                       @dt_vencimento,
                       @dt_pagamento,
                       @dt_insercao,
                       @dt_insercao_pagamento,
                       @referencia,
                       @cobrador_lancto,
                       @cobrador_pagto,
                       @usr_lancto,
                       @forma_pagto)";
  
          string Update = "UPDATE convidado_acesso SET dt_saida = @dt_saida, usr_saida= @usr_saida, status= @status, debpag = @deb WHERE nome = @nome AND debpag = 0";
          #endregion
  
          #region MySql
          MySqlConnection conexao = conexao = new MySqlConnection("Server= localhost; Database = crdores; Uid= root;Pwd = ik9rru2j;Allow Zero Datetime = true");
          MySqlCommand cmd = new MySqlCommand();
          cmd.Connection = conexao;
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = Select;
          MySqlDataAdapter da = new MySqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          conexao.Open();
          ta = conexao.BeginTransaction();
  
          #endregion
  
  
          try
          {
              #region TratamentoSelect
  
  
              List&lt;Convidado&gt; convidado = null;
              Convidado retorno = null;
              if ((ds.Tables.Count &gt; 0) &amp;&amp; (ds.Tables[0].Rows.Count &gt; 0))
              {
                  convidado = new List&lt;Convidado&gt;();
                  foreach (DataRow row in ds.Tables[0].Rows)
                  {
  
  
  
                      string status = "";
                      int deb = 0;
  
                      retorno = new Convidado();
                      try
                      {
                          retorno.Nome = row["nome"].ToString();
                      }
                      catch { }
                      try
                      {
                          retorno.Cod_Dep = Convert.ToInt32(row["cod_dependente"]);
                      }
                      catch { }
                      try
                      {
                          retorno.Dt_Insercao = Convert.ToDateTime(row["dt_insercao"]);
                      }
                      catch { }
                      try
                      {
                          retorno.Matricula = Convert.ToInt32(row["matricula"]);
                      }
                      catch { }
                      try
                      {
                          retorno.Id_Convidado = Convert.ToInt32(row["id_convidado"]);
                      }
                      catch { }
  
                      #region Variaveis
  
  
                      string tmp = BuscaTempoLimite();
                      int z = tmp.Length;
                      string h = tmp.Remove(1, z - 1);
                      string m = tmp.Remove(0, 2);
  
                      string saida = BuscaHoraServidor();
                      retorno.Dt_Insercao = retorno.Dt_Insercao.AddMinutes(15);
                      string entrada = retorno.Dt_Insercao.ToString("HH:mm");
                      DateTime insercao = Convert.ToDateTime(retorno.Dt_Insercao);
                      DateTime left = Convert.ToDateTime(saida);
                      saida = left.ToString("HH:mm");
  
                      int horaEntrada = Convert.ToInt32(entrada.Remove(2, 3));
                      int minutosEntrada = Convert.ToInt32(entrada.Remove(0, 3));
                      int horaSaida = Convert.ToInt32(saida.Remove(2, 3));
                      int minutosSaida = Convert.ToInt32(saida.Remove(0, 3));
                      TimeSpan first = new TimeSpan(horaEntrada, minutosEntrada, 0);
                      TimeSpan last = new TimeSpan(horaSaida, minutosSaida, 0);
                      TimeSpan result = last - first;
                      TimeSpan limite = new TimeSpan(Convert.ToInt32(h), Convert.ToInt32(m), 59);
  
                      int hora = last.Hours - first.Hours;
                      int minutosss = last.Minutes - first.Minutes;
  
                      result = new TimeSpan(hora, minutosss, 0);
                      #endregion
  
                      if (left.Day &lt;= insercao.Day)
                      {
                          if (result &gt; limite)
                          {
                              status = "Pago";
                              deb = 0;
                          }
                          else
                          {
                              status = "Devolvido";
                              deb = 2;
                          }
                      }
                      else
                      {
                          status = "Pago";
                      }
  
  
  
              #endregion
  
                      #region TratamentoInsert
  
                      cmd.CommandText = Insert;
  
                      #region váriaveis
                      int cash = 0;
                      string eventoF = "", usre = "", cobrador = "", tmpLimite = "";
                      int ide = 0;
                      ide = PegaMaxID();
                      string insercao_pagto;
                      string dt_lancamento = BuscaHoraServidor();
                      string saidaa = VerificaSaida(Convert.ToInt32(retorno.Id_Convidado));
                      string referencia = retorno.Dt_Insercao.ToString("MM/yyyy");
                      if (saida != "")
                      {
                          insercao_pagto = saida;
                      }
                      else
                      {
                          insercao_pagto = BuscaHoraServidor();
                      }
  
                      int tipo_pessoa;
                      if (retorno.Cod_Dep &gt; 0)
                      {
                          tipo_pessoa = 3;
                      }
                      else
                      {
                          tipo_pessoa = 1;
                      }
  
                      retorno.Dt_Insercao = retorno.Dt_Insercao.AddHours(1);
                      retorno.Dt_Insercao = retorno.Dt_Insercao.AddMinutes(15);
                      retorno.Dt_Insercao = retorno.Dt_Insercao.AddSeconds(59);
                      string ent = retorno.Dt_Insercao.ToString("dd/MM/yyyy");
                      #endregion
  
                      DataSet confg = pag.BuscaConfigs();
                      foreach (DataRow roww in confg.Tables[0].Rows)
                      {
                          eventoF = roww["ev_financeiro"].ToString();
                          cash = Convert.ToInt32(roww["valor"]);
                          usre = roww["usr_insercao"].ToString();
                          tmpLimite = roww["tempo_permanencia"].ToString();
                          cobrador = roww["cod_cobrador"].ToString();
                      }
  
                      cmd.Parameters.Add(new MySqlParameter("@numero_int", MySqlDbType.Int32)).Value = ide + 1; **//HERE THE ERROR STARTS**
                      cmd.Parameters.Add(new MySqlParameter("@parcela", MySqlDbType.VarChar)).Value = "1/1";
                      cmd.Parameters.Add(new MySqlParameter("@tipo_pessoa", MySqlDbType.Int32)).Value = tipo_pessoa;
                      cmd.Parameters.Add(new MySqlParameter("@matricula", MySqlDbType.Int32)).Value = retorno.Matricula;
                      cmd.Parameters.Add(new MySqlParameter("@cod_dependente", MySqlDbType.Int32)).Value = retorno.Cod_Dep;
                      cmd.Parameters.Add(new MySqlParameter("@ev_financeiro", MySqlDbType.Int32)).Value = eventoF;
                      cmd.Parameters.Add(new MySqlParameter("@quantidade", MySqlDbType.Int32)).Value = 1;
                      cmd.Parameters.Add(new MySqlParameter("@valor_unitario", MySqlDbType.Double)).Value = Convert.ToDouble(cash);
                      cmd.Parameters.Add(new MySqlParameter("@valor_total", MySqlDbType.Double)).Value = Convert.ToDouble(cash);
                      cmd.Parameters.Add(new MySqlParameter("@dt_lancamento", MySqlDbType.Date)).Value = Convert.ToDateTime(dt_lancamento); //  X
                      cmd.Parameters.Add(new MySqlParameter("@dt_vencimento", MySqlDbType.Date)).Value = ent; // X
                      cmd.Parameters.Add(new MySqlParameter("@dt_pagamento", MySqlDbType.Date)).Value = saida; //  X
                      cmd.Parameters.Add(new MySqlParameter("@dt_insercao", MySqlDbType.DateTime)).Value = saida; //  X
                      cmd.Parameters.Add(new MySqlParameter("@dt_insercao_pagamento", MySqlDbType.DateTime)).Value = insercao_pagto; // X
                      cmd.Parameters.Add(new MySqlParameter("@referencia", MySqlDbType.Date)).Value = referencia;
                      cmd.Parameters.Add(new MySqlParameter("@cobrador_lancto", MySqlDbType.VarChar)).Value = cobrador;
                      cmd.Parameters.Add(new MySqlParameter("@cobrador_pagto", MySqlDbType.VarChar)).Value = cobrador;
                      cmd.Parameters.Add(new MySqlParameter("@usr_lancto", MySqlDbType.VarChar)).Value = usre;
                      cmd.Parameters.Add(new MySqlParameter("@forma_pagto", MySqlDbType.Int32)).Value = 1;
                      cmd.ExecuteNonQuery();
  
                      #endregion
  
                      #region TratamentoUpdate
  
                      cmd.CommandText = Update;
                      cmd.Parameters.Add(new MySqlParameter("@dt_saida", MySqlDbType.DateTime)).Value = conv.Dt_Saida;
                      cmd.Parameters.Add(new MySqlParameter("@usr_saida", MySqlDbType.VarChar)).Value = usuario;
                      cmd.Parameters.Add(new MySqlParameter("@status", MySqlDbType.VarChar)).Value = status;
                      cmd.Parameters.Add(new MySqlParameter("@deb", MySqlDbType.Int32)).Value = deb;
                      cmd.Parameters.Add(new MySqlParameter("@nome", MySqlDbType.VarChar)).Value = retorno.Nome;
                      cmd.ExecuteNonQuery();
  
                      #endregion
  
                      ta.Commit();
  
                  }
              }
          }
  </code></pre>
  
  <p><code>Cmd.Parameter.Clear()</code> Is not a good practise. Here's the ANSWER. </p>
  
  <p>Outside the ForEachLoop :</p>
  
  <pre><code>Cmd.Parameter.Add(new MySqlParameter("@Hellow", MySqlDataType));
  </code></pre>
  
  <p>Inside the ForEachLoop</p>
  
  <pre><code>Cmd.Parameter["@Hellow"].Value = anyValueOrVariable;
  </code></pre>