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 delete code error

mysql delete code error

Scheduled Pinned Locked Moved C#
databasemysqlhelpcareer
21 Posts 4 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 N Mohamed rafi

    Sir, i have heard and tried what you said, the below coding is here; if i run coding it shows record deleted successfully but still the record not deleting from database using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Drawing; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Data.SqlClient; private void DisplayData() { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); DataTable dt = new DataTable(); adapt = new MySqlDataAdapter("select * from employee", cnn); adapt.Fill(dt); dataGridView1.DataSource = dt; cnn.Open(); cnn.Close(); } private void ClearData() { textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; ID = 0; } private void button9_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 = textBox6.Text; string name = textBox7.Text; string salary = textBox8.Text; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; string query = "DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary"; 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(); MessageBox.Show("Record Deleted Successfully");

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

    No, you haven't. Read what I said again ... and then look closely at your code ...

    "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
    • OriginalGriffO OriginalGriff

      Use the debugger. Put a breakpoint on the ExecuteNonQuery line:

      cmd.ExecuteNonQuery();

      and when it hits, look closely at exactly what is in the three variables id, name, and salary. Then go to your MySql management software and run a SELECT to seem how many records match that criteria. If it's zero, then no rows will be deleted. Back to Visual studio, and use step over to execute the ExecuteNonQuery line. Back to the DB management, and run the query again. How many rows this time? If it hasn't changed, then go back to your code and look at it really carefully ... does "-" have the same effect as "="?

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

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

      Sir, Pls understand i have tried what you said, first i check db has 13rows and now with cmd.ExecuteNonQuery(); this code running it shows record deleted successfully and db has no change, and i removed cmd.ExecuteNonQuery(); this line and run the code it shows the same, so pls change the code and give pls 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 = textBox6.Text; string name = textBox7.Text; string salary = textBox8.Text; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; string query = "DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary"; 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(); MessageBox.Show("Record Deleted Successfully"); cnn.Close(); DisplayData(); ClearData(); } } else { MessageBox.Show("Please Select Record to Delete"); } i think if i am doing wrong i dont know about breakpoint how to do that

      OriginalGriffO 1 Reply Last reply
      0
      • N N Mohamed rafi

        Sir, i have heard and tried what you said, the below coding is here; if i run coding it shows record deleted successfully but still the record not deleting from database using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Drawing; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Data.SqlClient; private void DisplayData() { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); DataTable dt = new DataTable(); adapt = new MySqlDataAdapter("select * from employee", cnn); adapt.Fill(dt); dataGridView1.DataSource = dt; cnn.Open(); cnn.Close(); } private void ClearData() { textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; ID = 0; } private void button9_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 = textBox6.Text; string name = textBox7.Text; string salary = textBox8.Text; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; string query = "DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary"; 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(); MessageBox.Show("Record Deleted Successfully");

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

        N Mohamed rafi wrote:

        if i run coding it shows record deleted successfully

        No it does not. Your code just displays the message "Record Deleted Successfully", but you have no idea whether it worked or not. If you read the documentation at SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient) | Microsoft Docs[^], you will see that it returns a value. If you do not check that return value, you have no idea if the delete worked. So stop guessing and use the tools that are available to you.

        N 1 Reply Last reply
        0
        • L Lost User

          N Mohamed rafi wrote:

          if i run coding it shows record deleted successfully

          No it does not. Your code just displays the message "Record Deleted Successfully", but you have no idea whether it worked or not. If you read the documentation at SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient) | Microsoft Docs[^], you will see that it returns a value. If you do not check that return value, you have no idea if the delete worked. So stop guessing and use the tools that are available to you.

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

          I don't unsderstand what i have to do sir, pls give step procedure

          L 1 Reply Last reply
          0
          • N N Mohamed rafi

            I don't unsderstand what i have to do sir, pls give step procedure

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

            N Mohamed rafi wrote:

            pls give step procedure

            1. Learn C# 2. Learn SQL 3. Learn debugging 4. and most important: use the documentation, especially when your code does not work. You need to understand that copying code from random sites on the internet is not going to teach you how to become a successful developer. It takes time, study, hard work and practice, practice, practice.

            1 Reply Last reply
            0
            • N N Mohamed rafi

              Sir, Pls understand i have tried what you said, first i check db has 13rows and now with cmd.ExecuteNonQuery(); this code running it shows record deleted successfully and db has no change, and i removed cmd.ExecuteNonQuery(); this line and run the code it shows the same, so pls change the code and give pls 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 = textBox6.Text; string name = textBox7.Text; string salary = textBox8.Text; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; string query = "DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary"; 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(); MessageBox.Show("Record Deleted Successfully"); cnn.Close(); DisplayData(); ClearData(); } } else { MessageBox.Show("Please Select Record to Delete"); } i think if i am doing wrong i dont know about breakpoint how to do that

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

              You really don't pay any attention to what people say, do you? All you want is somebody to fix your problem for you ... without any effort or thinking on your part. Sad, sad, sad ... So. A massive hint it is then. Step 1) Look at your code:

              DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary

              Step 2) Read this line of my previous post:

              Quote:

              If it hasn't changed, then go back to your code and look at it really carefully ... does "-" have the same effect as "="?

              Step 3) Put (1) and (2) together.

              "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

              N 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                You really don't pay any attention to what people say, do you? All you want is somebody to fix your problem for you ... without any effort or thinking on your part. Sad, sad, sad ... So. A massive hint it is then. Step 1) Look at your code:

                DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary

                Step 2) Read this line of my previous post:

                Quote:

                If it hasn't changed, then go back to your code and look at it really carefully ... does "-" have the same effect as "="?

                Step 3) Put (1) and (2) together.

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

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

                thank you so much, I am really mad, now I understand the error you said very well, thanks a lot, please say like this specific. it was a blender mistake

                OriginalGriffO 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  You really don't pay any attention to what people say, do you? All you want is somebody to fix your problem for you ... without any effort or thinking on your part. Sad, sad, sad ... So. A massive hint it is then. Step 1) Look at your code:

                  DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary

                  Step 2) Read this line of my previous post:

                  Quote:

                  If it hasn't changed, then go back to your code and look at it really carefully ... does "-" have the same effect as "="?

                  Step 3) Put (1) and (2) together.

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

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

                  pls suggest me i dont know how to mark this thread as solved?

                  N 1 Reply Last reply
                  0
                  • N N Mohamed rafi

                    pls suggest me i dont know how to mark this thread as solved?

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

                    Warning also showing that CS0414: the field form1.ID is assigned but its value is never used CS8618 Non nullable field adapt must contain non null value when exiting constructtor consider declaring the field as nullable using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Drawing; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Data.SqlClient; namespace Basic { public partial class Form1 : Form { MySqlDataAdapter adapt; int ID = 0; public Form1() { InitializeComponent(); DisplayData(); } private void DisplayData() { string connectionString; MySqlConnection cnn; connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql"; cnn = new MySqlConnection(connectionString); DataTable dt = new DataTable(); adapt = new MySqlDataAdapter("select * from employee", cnn); adapt.Fill(dt); dataGridView1.DataSource = dt; cnn.Open(); cnn.Close(); } private void ClearData() { textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; ID = 0; }

                    1 Reply Last reply
                    0
                    • N N Mohamed rafi

                      thank you so much, I am really mad, now I understand the error you said very well, thanks a lot, please say like this specific. it was a blender mistake

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

                      Details are important in development: you need to look in order to see what is there - or you don't find your problems. Pointing out the problem specifically doesn't teach you to do that - it teaches you to reply on others to do it for you, and you can't grew as a developer based on others doing the "hard bits" Debugging code is something we all do every day - often several times a day, and occasionally over a period of several days! So start learning how to fix a problem: gather evidence on what is happening, compare that with what you expected, and ask yourself "what do I need to know to find out why?" Then look for that: form an idea as to why it failed, and make a prediction as you what that implies. Then check that - the debugger is a good tool for that - and see if the way you expected the fault to happen checks out. Asking others without doing that doesn't help you, not in the long run - it just makes you more dependant on others to fix your problems, and that wastes time and make you look worse than you are. Have a go with the next one, and see how far you can get - it's not as difficult as it sounds if you just think carefully!

                      "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

                      N 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Details are important in development: you need to look in order to see what is there - or you don't find your problems. Pointing out the problem specifically doesn't teach you to do that - it teaches you to reply on others to do it for you, and you can't grew as a developer based on others doing the "hard bits" Debugging code is something we all do every day - often several times a day, and occasionally over a period of several days! So start learning how to fix a problem: gather evidence on what is happening, compare that with what you expected, and ask yourself "what do I need to know to find out why?" Then look for that: form an idea as to why it failed, and make a prediction as you what that implies. Then check that - the debugger is a good tool for that - and see if the way you expected the fault to happen checks out. Asking others without doing that doesn't help you, not in the long run - it just makes you more dependant on others to fix your problems, and that wastes time and make you look worse than you are. Have a go with the next one, and see how far you can get - it's not as difficult as it sounds if you just think carefully!

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

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

                        Ok sir

                        1 Reply Last reply
                        0
                        • N N Mohamed rafi

                          DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary this code is working but it shows record deleted successfully but in database its not deleted,, what i do?

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

                          You're not listening, and i'm not even entirely sure you even read entire replies to you. YOu're just looking for copy'n'paste code without any work on your part, that's where the problem lies. Where did those "AND" clauses come from? They're not in the SQL statement I wrote, and they are not needed at all since an ID uniquely identifies records.

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