in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

ArgumentNullException while trying to commit a transaction

Last post 04-19-2008 12:50 AM by gergelykiss. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-19-2008 12:37 AM

    ArgumentNullException while trying to commit a transaction

    Greetings to all of you!

    I've just started using SQLite in my open source invoice application and I always get an exception, when my code tries to commit the transaction, which should save the data of an invoice to the database.

     My code looks like this:

    class Invoice
    {
    public bool SaveToDatabase()
            {
                try    {
                    // Open database connection
                    SqliteConnection connection = SqliteFunctions.ConnectDB(false); // this is my own implementation for connecting - it just opens the connection, then returns the SqliteConnection object (that single boolean paramter tells the function whether to indicate errors to the user or not)
                   // Begin transaction
                   using ( SqliteTransaction dbtrans = connection.BeginTransaction() )
                    {
                        using ( SqliteCommand command = new SqliteCommand(connection) )
                        {
                            command.CommandText =
                                    "INSERT INTO szamlak (sorszam, ev, kelt, peldanyszam, allapotkod, teljesites_helye, teljesites_ideje, "+
                                    "fizetesi_mod, fizetesi_hatarido, megjegyzes, nyomtatva, kerekit) VALUES "+
                                    "(@sorszam, @ev, @kelt, @peldanyszam, @allapotkod, @telj_helye, @telj_ideje, "+
                                    "@fizetesi_mod, @fizetesi_hatarido, @megjegyzes, @nyomtatva, @kerekit)";
                            command.Prepare();
                            command.Parameters.AddWithValue("@sorszam", serial.GetNumber());
                            command.Parameters.AddWithValue("@ev", serial.GetYear());
                            command.Parameters.AddWithValue("@kelt", this.GetDate());
                            command.Parameters.AddWithValue("@peldanyszam", this.GetCopies().ToString());
                            if(this.isStorno)
                                command.Parameters.AddWithValue("@allapotkod", 3);
                            else
                                command.Parameters.AddWithValue("@allapotkod", 0);
                            command.Parameters.AddWithValue("@telj_helye", this.GetFulfillLocation());
                            command.Parameters.AddWithValue("@telj_ideje", this.GetFulfillDate());
                            command.Parameters.AddWithValue("@fizetesi_mod", this.GetPaymentMethod());
                            command.Parameters.AddWithValue("@fizetesi_hatarido", this.GetPayDate());
                            command.Parameters.AddWithValue("@megjegyzes", this.GetComment());
                            command.Parameters.AddWithValue("@nyomtatva", '0');
                            if(this.roundSum)
                                command.Parameters.AddWithValue("@kerekit",'1');
                            else
                                command.Parameters.AddWithValue("@kerekit",'0');
                            command.ExecuteNonQuery();

                            // Here comes a few other INSERT INTO queries, each one re-assigns the value of the command.CommandText property,
                            // and runs the query with command.ExecuteNonQuery();

                           }
                         // End of transaction
                        dbtrans.Commit();
                        }
                    }
                    connection.Close();
                    return true;
                }
                catch(SqliteException ex)
                {
                    PopupDialog.ShowErrorMessage("Database error",ex.Message); // drop a message to the user
                    return false;
                }
                catch(FormatException ex)
                {
                    PopupDialog.ShowErrorMessage("Wrong data type",ex.Message); // drop a message to the user
                    return false;
                }
            }

     My debugger tells me, that the problem is on the line 309, where the dbtrans.Commit() function call is. I wonder, what could be wrong with my code?

    The exception I get is the following:

    System.ArgumentNullException: Argument cannot be null.
    Parameter name: No connection associated with this transaction
      at Mono.Data.Sqlite.SqliteTransaction.IsValid (Boolean throwError) [0x00000]
      at Mono.Data.Sqlite.SqliteTransaction.Rollback () [0x00000]
      at System.Data.Common.DbTransaction.Dispose (Boolean disposing) [0x00000]
      at Mono.Data.Sqlite.SqliteTransaction.Dispose (Boolean disposing) [0x00000]
      at System.Data.Common.DbTransaction.Dispose () [0x00000]
      at Számlasegéd.Invoice.SaveToDatabase () [0x00777] in /home/kissg/szamlaseged/trunk/Functions/InvoiceGenerator.cs:309 

  • 04-19-2008 12:50 AM In reply to

    Re: ArgumentNullException while trying to commit a transaction

    It's a bit strange: if I don't put the BeginTransaction() call into a using statement, everything works fine. Maybe my SqliteTransaction object disposes for some reason, before it could commit... Any ideas?
Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems