I have a listbox and I let the user delete row in the listbox which deletes the row in the table, then refreshes the rowset and the repopulates the listbox. The problem is, it is repopulating the listbox before the record gets deleted from the table. Here is the code.
var SQL As String
Session.db_Data.BeginTransaction
for x as Integer = 1 to lbUsersInGroup.RowCount
if lbUsersInGroup.Selected(x-1) then
SQL = "Delete from GROUP_USERS where ID = "+lbUsersInGroup.RowTagAt(x-1)
Session.db_Data.ExecuteSQL(SQL)
end
Next
Try
Session.db_Data.CommitTransaction
Populate_UsersInGroup()
Catch e As DatabaseException
Session.db_Data.RollbackTransaction
end
How do I create a delay after the commit so the listbox doesn’t populate so quickly?
The problem is, it is repopulating the listbox before the record gets deleted from the table.
How do you know? Dump the number of rows in the table to System.DebugLog before your for loop and immediately after CommitTransaction to confirm the rows are deleted and that it occurs prior to your call to refresh the list.
As Greg said, using a prepared statement will be optimal.
As soon as you deleted the rows from the database, you don’t need to reload the entire listbox, just remove the affected rows with Listbox.RemoveRowAt
Make sure you loop from the end to the top:
for x as Integer = lbUsersInGroup.RowCount downto 1
If lbUsersInGroup.Selected(x-1) then
lbUsersInGroup.RemoveRowAt(x-1)
end
Next