Loop Through / Delete From ListBox

  • Thread starter Sparky Arbuckle
  • Start date
S

Sparky Arbuckle

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.
 
J

john_teague

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):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top