views:

551

answers:

1

I get the error SQLite error Insufficient parameters supplied to the command when running an insert statement in my program. However, I've got it narrowed down to it will only occur if I try to insert data into two different tables in succession. Meaning I insert data into one and everything in the method is diposed of since I am using USING statements.

This error won't occur if I insert data into one table stop debugging, rebuild my solution and then insert into the other table.

Anyone have any ideas? Like I said everything is encapsulated in using statements so I don't know what is getting held in memory.

Here is my method:

 public static void SQLiteTableINSERT(string tableName)
    {
        using (SQLiteConnection Conn = new SQLiteConnection(SQLiteConn.Conn))
        {
            using (SQLiteTransaction sqliteTrans = Conn.BeginTransaction())
            {
                using (SQLiteCommand cmd = Conn.CreateCommand())
                {
                    cmd.CommandText = PrepareInsert(tableName);

                    for (int i = 0; i < LocalDataSet.LocalDs.Tables[0].Rows.Count; ++i)
                    {
                        foreach (DataColumn col in LocalDataSet.LocalDs.Tables[0].Columns)
                        {
                            string temp = LocalDataSet.LocalDs.Tables[0].Rows[i][col, DataRowVersion.Current].ToString();
                            cmd.Parameters.AddWithValue("@" + col.ColumnName, temp);                                
                        } 

                        cmd.ExecuteNonQuery();                          
                    }                        
                }

                sqliteTrans.Commit();
            }
        }

        SQLite.SQLiteConn.Conn.Close();
    }

Any ideas would be great. Thanks.

A: 

I think you need to be using the tableName in the loop, assuming that the tables don't have the same schema.

The command is prepared for the table with name tableName, but the params you are adding are always from Tables[0], try Tables[tablename]

PhilO
In this case the string tableName is miss leading because it is actually the name of the excel file which gets imported. I never set the actual DataSet TableName to anything, that is why I just use the index.Does this really matter in the grand scheme of things?
Nathan