Monday, March 26, 2012

Loop Through / Delete From ListBox

I've created 2 listboxes with buttons that transfer items between the
two. The idea is that one of the listboxes will have a collection of
items to be deleted from the database. I haven't found any
documentation on how to loop through the listbox to build a query
string. I'm guessing it will look a little like [pseudo-code]:

Dim strSQL as string = "DELETE FROM tbl x,y,z WHERE "

For Each item in listbox2

i = i + 1

strSQL += "Name= '" & listbox2.item & "'"

If # of items in listbox2 > 1 Then
strSQL += " AND "
Else
strSQL += ";"
End If

Next

Could anyone help me with the syntax for this listbox. I'm trying to
read up on it but am having no luck.

Thanks in advance.I would use the IN statement that takes a list of id values.

string deleteIdList = "";
foreach(ListItem item in listBox.Items){
deleteIdList += item.Value + ",";
}
deleteIdList = deleteIdList.TrimEnd(',');
string deleteSql = String.Format("Delete from tbl where it in({0})",
deleteIdList):
I'm using VB.net

0 comments:

Post a Comment