Search code examples
formswindow

C# programming with window forms


private void dataGridView1_RowHeaderMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            int  ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());  
            txtName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();  
            txtFname.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtAddress.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("Update student set  name ='"+txtName.Text+"', fathername= '"+txtFname.Text+"', address= '"+txtAddress.Text+"' where id = ID", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Display();
            MessageBox.Show("Record is Updated");

When i run this code then my whole database table is updated with the current values and i can't understand the problem


Solution

  • "where id = ID" condition is always true so all records are affected. You need to actually set a value for "ID". Perhaps you need to write

    "where id =" + ID.ToString()