Reputation: 265
I wanted to delete a selected Item from a List control but can't. What's wrong with my code:
[Bindable]private var dp:ArrayCollection = new ArrayCollection();
private var conn:SQLConnection;
protected function Delete(event:MouseEvent):void {
Stmt = new SQLStatement();
Stmt.sqlConnection = conn;
Stmt.text = "DELETE FROM UserTable WHERE firstName="+listBox.selectedIndex;
Stmt.execute();
}
<s:List id="listBox" itemRenderer="UserRenderer"></s:List>
In UserRenderer:
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:Label text="{data.lastName}, {data.firstName}, {data.id}"/>
</s:ItemRenderer>
Upvotes: 0
Views: 447
Reputation: 11912
List#selectedIndex
refers to the position of the selected item in the dataprovider. The first element will have index 0, the second index 1 and so on. If no item is selected, selectedIndex
will be -1
.
If you want to select or delete by firstName - as you do in your query - you will have to pass in a valid first name instead of an index position. You can do this with the List#selectedItem
property. Also don't forget the single quotes in your query if you're not using query params.
"DELETE FROM UserTable " +
"WHERE firstName = '" + listBox.selectedItem.firstName + "'";
You weren't asking for this, but I'll tell you anyway: for security reasons you should use query params when using variables in your queries. One way to achieve this in ActionScript is:
stmt.parameters[0] = listBox.selectedItem.firstName;
stmt.text = "DELETE FROM UserTable WHERE firstName = ?";
(no single quotes required here)
Upvotes: 3