Mysql Delete Query Error on code

Mohamed Rafi N 106 Reputation points
2022-03-20T03:17:42.127+00:00

Mysql Delete record query error, It shows record deleted successfully but in mysql database it was not deleted in database. how to change query?

private void button9_Click(object sender, EventArgs e)
{
if (ID != 0)
//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");
}
}
}

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,326 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,415 questions
{count} votes

Accepted answer
  1. Rijwan Ansari 746 Reputation points MVP
    2022-03-20T17:07:29.32+00:00

    Hi @Mohamed Rafi N

    Please check below code

    private void button9_Click(object sender, EventArgs e)    
    {    
    if (ID != 0)    
    //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");    
    }    
    }    
    }    
    

    As per above code, please the query with values in Database check the value.

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

    In order to verify, please check data in database. Run below query with actual value and see if record comes.

    SELECT * FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary  
    

    Replace parameters with actual values.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Mohamed Rafi N 106 Reputation points
    2022-03-20T10:10:32.827+00:00

    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");
    cnn.Close();
    DisplayData();
    ClearData();
    }
    }
    else
    {
    MessageBox.Show("Please Select Record to Delete");
    }
    }