Mohamed Rafi

Mohamed Rafi

  • NA
  • 74
  • 8.9k

Query Error During Record Deleting

Mar 20 2022 12:31 PM

Sir, first i check db has 13rows after below code running it shows record deleted successfully and db has no change, so pls change the code and give pls

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

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


Answers (3)