0

So i have this "database is locked" exception since yesterday. Tried everything i found here, yet still nothing. I'm using two forms - simple login form and then another one, where user can add new product to database. Checking if product already exist in db works, but adding new one throws that exception. Check it out please:

try
{
    using (SqliteConnection db = new SqliteConnection("Filename=Magazyn.sqlite"))
    {
        db.Open();
        string SQLcheck = "select * from Administrator";
        using (SqliteCommand cmd = new SqliteCommand(SQLcheck, db))
        {
            using (SqliteDataReader reader = cmd.ExecuteReader())
            {
                var count = 0;
                while (reader.Read())
                {
                    count = count + 1;
                }

                if (count > 0 && textBox1 != null && !string.IsNullOrWhiteSpace(textBox1.Text))
                {
                    string sql = "select * from Administrator WHERE Name='" + textBox1.Text +
                                 "' AND Password ='" + textBox2.Text + "'";
                    using (SqliteCommand command = new SqliteCommand(sql, db))
                    {
                        using (SqliteDataReader rdr = command.ExecuteReader())
                        {
                            if (rdr.Read())
                            {
                                MessageBox.Show(textBox1.Text + ", zostałeś pomyślnie zalogowany", "Logowanie");
                                AddProduct a = new AddProduct();
                                a.ShowDialog();
                                this.Close();

                                return;
                            }
                            else
                            {
                                MessageBox.Show("Nie ma administratora o loginie " + textBox1.Text +" lub Twoje hasło jest niepoprawne", "Błąd logowania");
                                textBox1.Clear();
                                textBox2.Clear();


                            }

                        }
                    }
                }
                else if (count == 0)
                {
                    MessageBox.Show(
                        "W systemie nie istnieje konto administratora - nastąpi przekierowanie do formularza rejestracyjnego",
                        "Pierwsze logowania - konieczna rejestracja");
                    AddAdmin a = new AddAdmin();

                    a.ShowDialog();
                    return;
                }




            }

        }
     db.Close();  

    }

}
catch (Exception exception)
{
    Console.WriteLine(exception);
    throw;
}  

And 2nd Form:

try
{
    using (SqliteConnection db = new SqliteConnection("Filename = Magazyn.sqlite"))
    {
        db.Open();
        string sqlCheck = "select * from Produkty WHERE RFID='" + RFID.Text + "'";
        using (SqliteCommand cmd = new SqliteCommand(sqlCheck, db))
        {
            using (SqliteDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    MessageBox.Show("W bazie produktów jest już produkt o podanym tagu RFID", "Wykryto duplikat");
                    RFID.Clear();
                }
                else
                {
                    reader.Close();
                    reader.Dispose();
                    string sql = "INSERT INTO Produkty (Name, RFID, Price, Unit, VAT) values ('" + Nazwa.Text + "','" +
                                 RFID.Text + "','" + Cena.Text + "','" + Jednostka.Text + "','" + VATcat + "');";
                    //string sql = @"INSERT INTO Produkty (Name, RFID, Price, Unit, VAT) values (@name, @RFID, @price, @unit, @vat)";
                    using (SqliteCommand command = new SqliteCommand(sql, db))
                    {
                        using (SqliteDataReader rdr = command.ExecuteReader())
                        {
                            MessageBox.Show("Pomyślnie dodano produkt " + Nazwa.Text + " do bazy danych", "Dodano produkt");
                        }
                       /* command.CommandText = sql;
                        command.Connection = db;
                        command.Parameters.Add(new SqliteParameter("@name", Nazwa.Text));
                        command.Parameters.Add(new SqliteParameter("@RFID", RFID.Text));
                        command.Parameters.Add(new SqliteParameter("@price", Cena.Text));
                        command.Parameters.Add(new SqliteParameter("@unit", Jednostka.Text));
                        command.Parameters.Add(new SqliteParameter("@vat", VATcat));
                        command.ExecuteNonQuery();
                        MessageBox.Show("Pomyślnie dodano produkt " + Nazwa.Text + " do bazy danych", "Dodano produkt");
                       */ 
                    }
                }

            }

        }
        db.Close();
    }

}
catch (Exception exception)
{
    Console.WriteLine(exception);
    throw;
}

I commented another way of adding to db (Will test both when I get rid of that exception) Funny thing that if i change INSERT to SELECT it's working. If I use INSERT query directly in database file it's working too.

  • use ExecuteNonQuery for inserting data, https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx – kriss Feb 01 '18 at 15:57
  • This won't help fix your problem, but please don't do this: `"select * from Administrator WHERE Name='" + textBox1.Text + "' AND Password ='" + textBox2.Text + "'"`. Please learn and use the Parameters property on the sql command: https://stackoverflow.com/questions/4056872/how-to-pass-variable-into-sqlcommand-statement-and-insert-into-database-table – Jordan S. Jones Feb 01 '18 at 16:32
  • It's still the same exception, app throw it in first Form (where user log in , yet after i try to add product in 2nd Form – Jakub Sobański Feb 01 '18 at 16:36
  • You are using the second connection/command while the first one is still active. – CL. Feb 01 '18 at 17:49
  • Do you mean i should use cmd.Cancel() and .Dispose before second command ? – Jakub Sobański Feb 01 '18 at 18:31

0 Answers0