0

I need help please: I've two methods -one for insert data and one for update data- in my code and I call in first the "insert method" and in second time I call the "update method" but, in the second case, i recieve a "lock database error".

Here the methods:

  private bool mappingFileInDB(string numRep, string siglaNot, string annoPrat, string ErrorImporting, string fishedOut)
  {
        using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=" + pathProjectDir + @"\db\db.dat;"))
        {
            sqlite_conn.Open();
            using (SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO working_table (numero_repertorio, sigla_notaio, codice_pratica, anno_cartella, is_file_imported, if_error_importing, is_file_glyphed, is_cc_in_outFolder, is_td_in_toSignFolder, is_td_signed, is_td_in_signedFolder, is_td_in_notaroFolder, is_fishedOut ) VALUES (@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13)", sqlite_conn))
            {
                insertSQL.Parameters.Add(new SQLiteParameter("@1", numRep));
                insertSQL.Parameters.Add(new SQLiteParameter("@2", siglaNot));
                insertSQL.Parameters.Add(new SQLiteParameter("@3", ""));
                insertSQL.Parameters.Add(new SQLiteParameter("@4", annoPrat));
                insertSQL.Parameters.Add(new SQLiteParameter("@5", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@6", ErrorImporting));
                insertSQL.Parameters.Add(new SQLiteParameter("@7", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@8", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@9", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@10", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@11", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@12", "0"));
                insertSQL.Parameters.Add(new SQLiteParameter("@13", fishedOut));
                try
                {
                    insertSQL.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    sqlite_conn.Close();
                    sqlite_conn.Dispose();
                }
            }

        }
        GC.Collect();
        return true;
 }

 public bool updateFileInDB(string nameColumn, string newVal, string numRep, string siglaNot)
    {
        using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=" + pathProjectDir + @"\db\db.dat;"))
        {
            sqlite_conn.Open();
            using (SQLiteCommand updateSQL = new SQLiteCommand("UPDATE working_table SET " + nameColumn + " = @val WHERE numero_repertorio = @nR AND sigla_notaio = @sN", sqlite_conn))
            {
                updateSQL.Parameters.Add(new SQLiteParameter("@val", newVal));
                updateSQL.Parameters.Add(new SQLiteParameter("@nR", numRep));
                updateSQL.Parameters.Add(new SQLiteParameter("@sN", siglaNot));

                try
                {
                    updateSQL.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    sqlite_conn.Close();
                    sqlite_conn.Dispose();
                }
            }
        }
        GC.Collect();
        return true;
    }

Here the error:

Error screenshot

PS: if I call for more times the insert method it works correctly; if I call only the update method - without inserting something before - it works; it does'nt work only if I call in first the insert method and in the second time the update method.

I don't understand where's the problem!

aydinugur
  • 1,208
  • 2
  • 14
  • 21
Berat Rahmani
  • 113
  • 1
  • 8

1 Answers1

0

Now it works! I changed this:

insertSQL.ExecuteNonQuery();

with this:

insertSQL.ExecuteScalar();

and works fine!

Could anyone please tell me why?

Berat Rahmani
  • 113
  • 1
  • 8