Mohamed Rafi

Mohamed Rafi

  • NA
  • 74
  • 9.1k

MySQL Update Query Error

Mar 20 2022 4:02 PM

It shows record updated successfully but not updating in database

private void button13_Click(object sender, EventArgs e)
{
if (textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
//string id = Convert.ToInt32(textBox6.Text);
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "update employee set employee_id='" + this.textBox6.Text + "',employee_name='" + this.textBox7.Text + "',employee_salary='" + this.textBox8.Text + "' where employee_id='" + this.textBox6.Text + "';";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to update row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Record Updated Successfully");
cnn.Close();
DisplayData();
ClearData();
}
else if (dr == DialogResult.No)
{
MessageBox.Show("Dude, We keep this record as same");
}

}
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}


Answers (1)