Reputation: 346
I wrote the code for the update button and the delete button. My purpose is update and delete the datas that I've inserted into the database (that was created in mysql with phpmyadmin).
Update button code:
void ModificaBtnClick(object sender, EventArgs e)
{
Connessione.Open();
MySqlDataAdapter SDA=new MySqlDataAdapter("UPDATE INTO GARA set nome_gara='"+textBox1.Text+"',giudice='"+textBox2.Text+"',località='"+textBox3.Text+"',data='"+textBox4.Text+"',tpsopm='"+textBox5.Text+"',tpmopm='"+textBox6.Text+"',tpstot='"+textBox7+"',tpmtot='"+textBox8.Text+"')VALUES'"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+textBox8.Text+"')",Connessione);
SDA.SelectCommand.ExecuteNonQuery();
Connessione.Close();
MessageBox.Show("Dati modificati correttamente!");
}
Delete button code:
void CancellaBtnClick(object sender, EventArgs e)
{
Connessione.Open();
MySqlDataAdapter SDA=new MySqlDataAdapter("DELETE FROM GARA(nome_gara,giudice,località,data,tpsopm,tpmopm,tpstot,tpmtot)VALUES'"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+textBox8.Text+"')",Connessione);
SDA.SelectCommand.ExecuteNonQuery();
Connessione.Close();
MessageBox.Show("Dati cancellati correttamente!");
}
I haven't syntax error, but when I run the program and I try to modify or delete the data, the compiler show me an unhandled exception (An Unhandled exception of type MySql.Data.MySqlClient.MySqlException was thrown). When I quit the window this line is selected SDA.SelectCommand.ExecuteNonQuery();
I use the same instruction for the insert button, but it works.
Can you help me, please?
Edit: I've tried your solutions, but the values don't update into the db. I can modify them into the dataGridView, but when I press the update button they don't change
Upvotes: 1
Views: 5338
Reputation: 56697
I don't mean to sound harsh, but your code is not very good. Points to improve:
MySqlDataAdapter
when you don't need toThese three points will lead to the following code:
void ModificaBtnClick(object sender, EventArgs e)
{
Connessione.Open();
MySqlCommand cmd = new MySqlCommand("UPDATE GARA set nome_gara=@nomegara,giudice=@giudice,località=@localita,data=@data ...",Connessione);
cmd.Parameters.AddWithValue("@nomegara", textBox1.Text);
cmd.Parameters.AddWithValue("@giudice", textBox2.Text);
...
cmd.ExecuteNonQuery();
MessageBox.Show("Dati modificati correttamente!");
}
void CancellaBtnClick(object sender, EventArgs e)
{
Connessione.Open();
MySqlCommand cmd =new MySqlCommand("DELETE FROM GARA WHERE Field = @value, ...",Connessione);
cmd.Parameters.AddWithValue("@value", ...);
...
cmd.ExecuteNonQuery();
MessageBox.Show("Dati cancellati correttamente!");
}
Upvotes: 2
Reputation: 133370
Update don't need INTO but only
UPDATE GARA
SET set nome_gara='"+textBox1.Text+"'
,giudice='"+textBox2.Text+"'
,località='"+textBox3.Text+"'
,.....
eventulally add where condition for update the rows you need
Delete don't need value you delete the rows with match where condition eg:
DELETE FROM GARA
WHERE nome_gara='"+textBox1.Text+"'"
be sure for the proper where condition
and be careful in string concat that you are at risk for sql injections
Upvotes: 2