I am getting an out of bounds exception, when I run this sqlite command:
deleteTheRecordString = "Delete From ToDoTasks where Task like '"+ToDoWindow.TaskListBox.CellTextAt ( ToDoWindow.TaskListBox.SelectedRowIndex,1) +”'"
My command appears to be ok; I run a similar command in my SQL DB manager, and it works fine, so I don’t think the - format- (SQLite command proper) is correct.
Yes I saw that when I did a debug.
But if I select a row in a listbox, should not the selected row index be the number of the row I selected?
It does not appear to be…
But I have an idea of something to try.
Will advise after I try it.
Before any row is selected the SelectedRowIndex is -1 and your code may get executed prematurely, i.e. before any selection was made. I’ve learned to code defensively and never assume that SelectedRowIndex will be greater than or equal to 0, but explicitly test whether it really is.
One could say that 0 is the number of the first row …
But that’s not the issue here. Any code that directly passes the result of SelectedRowIndex to CellTextAt (or indeed any of the …At methods) is suspect as it assumes that this result is the number of a selected row – when in fact it could also be -1, as an indication of no row being selected.
This is an easy one
Take for example your current Code:
deleteTheRecordString = "Delete From ToDoTasks where Task like '"+ToDoWindow.TaskListBox.CellTextAt ( ToDoWindow.TaskListBox.SelectedRowIndex,1) +”'"
would become:
// db is your Database Connection
db.ExecuteSQL("Delete From ToDoTasks where Task like ?", ToDoWindow.TaskListBox.CellTextAt (ToDoWindow.TaskListBox.SelectedRowIndex,1) )
This was already mentioned by @Rick_Araujo in the first reply in this thread, and my post is exclusively about switching to PreparedStatements…
And simply pointing it out without suggesting a solution isn’t very helpful.
Whatever. Just to make you happy:
If ToDoWindow.TaskListBox.SelectedRowIndex <> DesktopListBox.NoSelection Then
db.ExecuteSQL("Delete From ToDoTasks where Task like ?", ToDoWindow.TaskListBox.CellTextAt (ToDoWindow.TaskListBox.SelectedRowIndex,1) )
End If
And please don’t mention that it’s better to assign a method to the window instead of directly accessing a window object, or that even with SQLite you should pay attention to the encoding in case the data comes from an external source, and so on. That’s not what this Thread is about…