Leonardo Soares
Leonardo Soares

Reputation: 33

Trouble Deleting entries from an access database using C# (Visual Studio)

I'm Having some trouble deleting an entry on my database. I can insert data, but i can't delete them.

I have a 2 variables database, and i want to manage those data.

but when i debug the program , the first button (btnAdicionar) works fine, but when i press the button "btnRemover", i get an erron on the line "cmd.ExecuteNonQuery();"

what am i doing wrong? thanks

here is the code:

    private void btnAdicionar_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\BancodeDados\\Nomes.mdb");
        string sql = "INSERT INTO Nomes (Nome, Sobrenome) VALUES(?, ?)";
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("Nome", txtNome.Text);
        cmd.Parameters.AddWithValue("Sobrenome", txtSobre.Text);
        cmd.ExecuteNonQuery();
        conn.Close();
        this.nomesTableAdapter.Fill(this.nomesDataSet.Nomes);
    }

    private void btnRemover_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\BancodeDados\\Nomes.mdb");
        string sql = "DELETE FROM Nomes (Nome, Sobrenome) WHERE (?, ?)";
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("Nome", txtNome.Text);
        cmd.Parameters.AddWithValue("Sobrenome", txtSobre.Text);
        cmd.ExecuteNonQuery();
        conn.Close();
        this.nomesTableAdapter.Fill(this.nomesDataSet.Nomes);
    }

Upvotes: 0

Views: 1233

Answers (1)

Icarus
Icarus

Reputation: 63970

Your delete statement is not valid SQL, hence the error when you call ExecuteNonQuery

It should be something like this:

DELETE FROM Nomes WHERE Nome= ? and Sobrenome = ? 

Upvotes: 6

Related Questions