My delete button is giving error expects the parameter

4

When I try to make a DELETE the following message is displayed:

Mycode:

privatevoidBtnExcluir_Click(objectsender,EventArgse){if(MessageBox.Show("Confirma a Exclusão desses Registros?", "Atenção",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        conexao.ConnectionString = strconexao;
        cmd.Connection = conexao;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "DELETE FROM PRODUTOS WHERE CODPROD =Codigo";

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@Codigo", chaveID);
        try
        {
            conexao.Open();
             cmd.ExecuteNonQuery();
             MessageBox.Show("Registros Excluidos com Sucesso!!!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Atenção");
        }
        finally
        {
            if (conexao.State == ConnectionState.Open)
            {
                conexao.Close();
                MontarLista();

                LimparFormulario();
                BtnExcluir.Enabled = true;
            }
         }
    }
}               
    
asked by anonymous 27.06.2015 / 23:52

1 answer

0
___ erkimt ___ My delete button is giving error expects the parameter ______ qstntxt ___

When I try to make a @ the following message is displayed:

Mycode:

privatevoidBtnExcluir_Click(objectsender,EventArgse){if(MessageBox.Show("Confirma a Exclusão desses Registros?", "Atenção",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        string sqlQuery = "DELETE FROM PRODUTOS WHERE CODPROD = @Codigo;";

        using (SqlConnection conexao = new SqlConnection(strconexao))
        {
            SqlCommand command = new SqlCommand(sqlQuery, conexao);
            command.Parameters.AddWithValue("@Codigo", chaveID);
            try
            {
                conexao.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("Registros Excluidos com Sucesso!!!");

                MontarLista();

                LimparFormulario();
                BtnExcluir.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Atenção");
            }
        }
    }
}        
    
______ ___ azszpr71588

I made some changes.

1. I got the% w / w that was missing from the query.

2. I created a new SqlCommand instance.

private void BtnExcluir_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Confirma a Exclusão desses Registros?", "Atenção",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        string sqlQuery = "DELETE FROM PRODUTOS WHERE CODPROD = @Codigo;";

        using (SqlConnection conexao = new SqlConnection(strconexao))
        {
            SqlCommand command = new SqlCommand(sqlQuery, conexao);
            command.Parameters.AddWithValue("@Codigo", chaveID);
            try
            {
                conexao.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("Registros Excluidos com Sucesso!!!");

                MontarLista();

                LimparFormulario();
                BtnExcluir.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Atenção");
            }
        }
    }
}        
    
___
28.06.2015 / 00:12