Nuri Engin Nuri Engin - 4 years ago 101
C# Question

C# Winform MySql Update Button error (it's update all records)

UPDATE: Problem solved; dear all with your help i just realized the problem and first i added ID column from db to datagridview as well i added a textbox to my formdesing. After that i just put "where" and "id" condition as below. Thanks you all!

string Query = "update doguAkdenizApp.team set id='" + this.txtEkipKayitNo.Text + "', name='" + this.txtEkipIsim.Text + "', surname='" + this.txtEkipSoyisim.Text + "', birth='" + this.dtpEkipDogum.Text + "', telephone='" + this.txtEkipTelefon.Text + "', email='" + this.txtEkipEposta.Text + "', city='" + this.cbEkipSehir.Text + "', adress='" + this.txtEkipAdres.Text + "', recorddate='"+this.dtpEkipDogum.Text+ "' where id='" + this.txtEkipKayitNo.Text + "';";


I am having problem with code block below, I am using an "Update" button to edit selected row's data but unfortunetly it's update whole records inserted to database.

How can i arrenge the code for only selected row?

private void btnEkipGuncelle_Click(object sender, EventArgs e)
{ string myConnection = "datasource=root;port=root;username=root;password=root";
string Query = "update doguAkdenizApp.team set name='" + this.txtEkipIsim.Text + "', surname='" + this.txtEkipSoyisim.Text + "', birth='" + this.dtpEkipDogum.Text + "', telephone='" + this.txtEkipTelefon.Text + "', email='" + this.txtEkipEposta.Text + "', city='" + this.cbEkipSehir.Text + "', adress='" + this.txtEkipAdres.Text + "', recorddate='"+this.dtpEkipDogum.Text+"';";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myReader;
try
{
myConn.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Güncelleme başarılı!"); //Update success notification
while (myReader.Read()) { }
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
loadTable();
clearAllTxt();*/
}


Thanks a lot, Nuri.

Answer Source
update doguAkdenizApp.team set name='' where some condition

You forgot the where condition. Right now you write to update the whole rows in the table, if you will add a where condition like where id = 3, then only the row with id (only for example) will be update

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download