Mysql Update Query Error
-
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"); } }
-
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"); } }
Did you try the technique OriginalGriff recommended here: [Re: mysql delete code error - C# Discussion Boards](https://www.codeproject.com/Messages/5867105/Re-mysql-delete-code-error) ?
-
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"); } }
-
Did you try the technique OriginalGriff recommended here: [Re: mysql delete code error - C# Discussion Boards](https://www.codeproject.com/Messages/5867105/Re-mysql-delete-code-error) ?
Yes sir. Output came. Thanks to all
-
Dude. No. Please burn it.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
Noted sir
-
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"); } }
I thought we discussed this ... your reply less than 5 hours ago was "Ok sir" :sigh: Is there any point in us talking to you at all?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I thought we discussed this ... your reply less than 5 hours ago was "Ok sir" :sigh: Is there any point in us talking to you at all?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
if (textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "")
No. Nuke it from orbit. That's the only way to be sure.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
if (textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "")
No. Nuke it from orbit. That's the only way to be sure.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
I don't think he really wants to learn - just get his homework done for him ... where he can't copy'n'paste a solution. :sigh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
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"); } }
So, it's obvious you're just copying and pasting code from the web and hoping it works. You don't have any idea why there are parameter objects or how they are used in the query.
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);You're using the TextBox values directly in your query. NEVER DO THIS! User input should be treated like it's the spawn of Satan. Using it directly in your query will lead to SQL Injection Attacks and you risk destroying your database doing that. Next, you NEVER change or update the value of an ID field in a table. Doing so will destroy your data integrity since records in one table will no longer relate to data in another table. Your query should look like this (and don't even think of copying and pasting this code!) Try to figure out what the code is doing.
string query = "UPDATE employee SET employee_name=@empName, employee_salary=@empSalary WHERE employee_id=@empId";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@empId", id);
cmd.Parameters.AddWithValue("@empName", name);
cmd.Parameters.AddWithValue("@empSalary", salary);I'm ignoring that you're storing salary values as text.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
So, it's obvious you're just copying and pasting code from the web and hoping it works. You don't have any idea why there are parameter objects or how they are used in the query.
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);You're using the TextBox values directly in your query. NEVER DO THIS! User input should be treated like it's the spawn of Satan. Using it directly in your query will lead to SQL Injection Attacks and you risk destroying your database doing that. Next, you NEVER change or update the value of an ID field in a table. Doing so will destroy your data integrity since records in one table will no longer relate to data in another table. Your query should look like this (and don't even think of copying and pasting this code!) Try to figure out what the code is doing.
string query = "UPDATE employee SET employee_name=@empName, employee_salary=@empSalary WHERE employee_id=@empId";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@empId", id);
cmd.Parameters.AddWithValue("@empName", name);
cmd.Parameters.AddWithValue("@empSalary", salary);I'm ignoring that you're storing salary values as text.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakNoted with thanks. its works
-
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"); } }
This could hardly be more wrong
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to update row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Record Updated Successfully");You call
ExecuteNonQuery
but ignore the return value, so if it failsyou will never know. You then ask the user whether to update the row, which (you think) you already did. And if the user answers "Yes", you then post a message to say the update succeeded. -
This could hardly be more wrong
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to update row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Record Updated Successfully");You call
ExecuteNonQuery
but ignore the return value, so if it failsyou will never know. You then ask the user whether to update the row, which (you think) you already did. And if the user answers "Yes", you then post a message to say the update succeeded.It works. thanks so much
-
It works. thanks so much
-
It works. thanks so much
I seriously doubt it works in all situations. Just because you get a dialog box that says the record is updated, doesn't mean your logic to that point is correct. Given your history, I think it's still very, very wrong.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I don't think he really wants to learn - just get his homework done for him ... where he can't copy'n'paste a solution. :sigh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Where would one start? At
textBox7
, or the missing usings? I'd burn the code and never look back.Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Where would one start? At
textBox7
, or the missing usings? I'd burn the code and never look back.Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
I say we take off and nuke the entire site from orbit. It's the only way to be sure.[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!