Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mysql Update Query Error

Mysql Update Query Error

Scheduled Pinned Locked Moved C#
databasemysqlhelpquestionannouncement
16 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N Mohamed rafi
    wrote on last edited by
    #1

    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"); } }

    V L OriginalGriffO D 5 Replies Last reply
    0
    • N N Mohamed rafi

      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"); } }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      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) ?

      N 1 Reply Last reply
      0
      • N N Mohamed rafi

        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"); } }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • V Victor Nijegorodov

          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) ?

          N Offline
          N Offline
          N Mohamed rafi
          wrote on last edited by
          #4

          Yes sir. Output came. Thanks to all

          1 Reply Last reply
          0
          • L Lost User

            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.

            N Offline
            N Offline
            N Mohamed rafi
            wrote on last edited by
            #5

            Noted sir

            1 Reply Last reply
            0
            • N N Mohamed rafi

              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"); } }

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              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 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

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                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!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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.

                OriginalGriffO 1 Reply Last reply
                0
                • L Lost User

                  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.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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!

                  "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

                  L 1 Reply Last reply
                  0
                  • N N Mohamed rafi

                    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"); } }

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    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

                    N 1 Reply Last reply
                    0
                    • D 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 Kreskowiak

                      N Offline
                      N Offline
                      N Mohamed rafi
                      wrote on last edited by
                      #10

                      Noted with thanks. its works

                      1 Reply Last reply
                      0
                      • N N Mohamed rafi

                        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"); } }

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        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.

                        N 1 Reply Last reply
                        0
                        • L Lost User

                          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.

                          N Offline
                          N Offline
                          N Mohamed rafi
                          wrote on last edited by
                          #12

                          It works. thanks so much

                          L D 2 Replies Last reply
                          0
                          • N N Mohamed rafi

                            It works. thanks so much

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Maybe.

                            1 Reply Last reply
                            0
                            • N N Mohamed rafi

                              It works. thanks so much

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                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!

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                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.

                                OriginalGriffO 1 Reply Last reply
                                0
                                • L Lost User

                                  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.

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #16

                                  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!

                                  "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

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups