Resolved SQLite Delete data don't work

Kostakis45

Active member
Joined
Apr 3, 2022
Messages
37
Programming Experience
Beginner
I'm trying to delete a record from database.
Delete_Redord.cs:
public void Delete_Info_From_DB()
        {
            Plasteka pa = new Plasteka();//Main Form

            var con = new SQLiteConnection(connection);
            con.Open();
            var cmd = new SQLiteCommand(con);
         
            try
            {
                cmd.CommandText = "DELETE FROM Description WHERE Mould_Code = '"+pa.Search_Box.Text+"'";//Am I missing something here? BTW Mould_Code is PK integer.
                cmd.ExecuteNonQuery();
            }
            catch
            {
                if ((cmd.CommandText = "SELECT * FROM Description WHERE Mould_Code = '" + pa.Search_Box.Text + "'") == null)
                {
                    MessageBox.Show("Επιτυχής διαγραφή.");

                    pa.mould_code_input.Text = null;
                    pa.machine_number_input.Text = null;
                    pa.machine_type_input.Text = null;
                    pa.supplier_input.Text = null;
                    pa.colour_input.Text = null;
                    pa.comboBox1.Text = null;
                    pa.comboBox2.Text = null;
                    pa.comboBox3.Text = null;
                    pa.numericUpDown1.Text = null;
                    pa.numericUpDown2.Text = null;
                    pa.numericUpDown3.Text = null;
                    pa.dateTimePicker1.Text = null;
                    pa.item_name_input.Text = null;
                    pa.pictureBox1.Image = null;
                    pa.pictureBox2.Image = null;
                }
                else
                {
                    MessageBox.Show("Αποτυχία διαγραφής.");
                }
            }    
            con.Close();
        }
My command line seems fine and I'm guessing that the problem lies in the reference I'm doing to get the value from textbox.But I can't figure out why it's not showing any errors,instead the program freezes.
Any idea or suggestion?
 
At this point, the only thing I can think of is you have an open connection from another part of your code that is locking the file.

I suggest creating a minimal program to reproduce the problem, and then post that code. That could should simply call your class method for deletion. Ideally your class should simply take the mould code number, but if you insist on putting that value into a text box, and then passing that text box to your class, then so be it.
I have put together all your suggestions so far except the ideally part wich I don't think i understand clearly but I'm assuming you are talking about return value?Also I fixed all my classes so that the connection could close everytime after executing.And i put my conections into a using statement to assure that it will surely close.
 
I found my problem.I was forgeting to close the SqliteDataReader in my search methods.Thanks everyone for the help.
This is a perfect example of why you should ALWAYS create disposable objects with a using statement. That way, you can't possibly forget to close/dispose the object.
 
+1. There are times when syntactic sugar actually helps you be a better programmer. Always use using with objects that implement IDisposable.

(With the exception of HttpClient where the recommendation is to create just a single static instance for use of the lifetime of the app, and which should only disposed when shutting down the app.)
 

Similar threads

Latest posts

Back
Top Bottom