2022-04-21

Delete primary key row from SQLite doesn't work

As my title describes I have a problem to delete from SQLite db. Let me explain.
1st It's a Windows Forms Application.
2nd .NET Framework 4.8
3d Nugget package SQLite Core

My Create, Insert, Search to db works fine. I have a search which is a textBox and when the user is searching something and gets back the correct info from db it gives them an option to Update(Not implemented yet) or to Delete with 2 separate buttons(obviously). Now, my delete method gets the value from the search box and tries to delete the current row (note that the search value is a primary key from db).

When I press the Delete button my program freezes without showing immediate error, while still in process of debugging after a while it shows my that the database is locked. CallStack says that it's going for sleep. That error occurs at the cmd.ExecuteNonQuery line.

I have searched all the web and here for related problems without finding something to suit me. I have an open post about this same problem in C# Forums for begginers. Haven't found a solution yet with some people trying to help me. I'm not well versed with SQLite. I hope I provided enough information.

Here is what I'm trying this far. Delete.cs

class Delete_Record
    {        
        string path = "Injection_Settings.db";
        public static TextBox Search_Box = new TextBox();
        public static TextBox mould_code_input = new TextBox();
        public static TextBox machine_number_input = new TextBox();
        public static TextBox machine_type_input = new TextBox();
        public static TextBox supplier_input = new TextBox();
        public static ComboBox colour_input = new ComboBox();
        public static ComboBox comboBox1 = new ComboBox();
        public static ComboBox comboBox2 = new ComboBox();
        public static ComboBox comboBox3 = new ComboBox();
        public static NumericUpDown numericUpDown1 = new NumericUpDown();
        public static NumericUpDown numericUpDown2 = new NumericUpDown();
        public static NumericUpDown numericUpDown3 = new NumericUpDown();
        public static DateTimePicker dateTimePicker1 = new DateTimePicker();
        public static TextBox item_name_input = new TextBox();
        public static PictureBox pictureBox1 = new PictureBox();
        public static PictureBox pictureBox2 = new PictureBox();

        public void Delete_Info_From_DB()
        {            
            int Mould_Code = Int32.Parse(Search_Box.Text);
            
            try
            {
                using (SQLiteConnection con = new SQLiteConnection(@"Data Source = " + path))
                {
                    con.Open();

                    using (SQLiteCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = @"DELETE FROM Description WHERE Mould_Code =@Mould_Code";
                        cmd.Prepare();
                        cmd.Parameters.AddWithValue("@Mould_Code", Mould_Code);
                        cmd.ExecuteNonQuery();

                        if ((cmd.CommandText = @"SELECT * FROM Description WHERE Mould_Code = " + Mould_Code + "") == null)
                        {
                            MessageBox.Show("Επιτυχής διαγραφή.");

                            mould_code_input.Text = null;
                            machine_number_input.Text = null;
                            machine_type_input.Text = null;
                            supplier_input.Text = null;
                            colour_input.Text = null;
                            comboBox1.Text = null;
                            comboBox2.Text = null;
                            comboBox3.Text = null;
                            numericUpDown1.Text = null;
                            numericUpDown2.Text = null;
                            numericUpDown3.Text = null;
                            dateTimePicker1.Text = null;
                            item_name_input.Text = null;
                            pictureBox1.Image = null;
                            pictureBox2.Image = null;
                        }    
                    }
                    con.Close();
                }                
            }
            catch (Exception)
            {               
                MessageBox.Show("Αποτυχία διαγραφής.");                
            }
        }      
    }

Delete Button in Form1

private void Delete_Record_Click(object sender, EventArgs e)
        {
            Delete_Record dr = new Delete_Record();
            Delete_Record.Search_Box.Text = Search_Box.Text;
            Delete_Record.mould_code_input = mould_code_input;
            Delete_Record.machine_number_input = machine_number_input;
            Delete_Record.machine_type_input = machine_type_input;
            Delete_Record.supplier_input = supplier_input;
            Delete_Record.colour_input = colour_input;
            Delete_Record.comboBox1 = comboBox1;
            Delete_Record.comboBox2 = comboBox2;
            Delete_Record.comboBox3 = comboBox3;
            Delete_Record.numericUpDown1 = numericUpDown1;
            Delete_Record.numericUpDown2 = numericUpDown2;
            Delete_Record.numericUpDown3 = numericUpDown3;
            Delete_Record.dateTimePicker1 = dateTimePicker1;
            Delete_Record.item_name_input = item_name_input;
            Delete_Record.pictureBox1 = pictureBox1;
            Delete_Record.pictureBox2 = pictureBox2;

            if (Database_Search.Search_Box.Text != null)
            {
                dr.Delete_Info_From_DB();
            }            
        }


No comments:

Post a Comment