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>
List#selectedIndexrefers 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,selectedIndexwill 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#selectedItemproperty. Also don’t forget the single quotes in your query if you’re not using query params.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:
(no single quotes required here)