Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to delete from database(access) multiple records which are matching to a listbox(in c#)
so how can i do that?
Posted
Comments
Michael Waguih 24-Jan-11 5:06am    
Do you mean you want to delete the selected items in the list box from the database ?

The listbox stores only plain text, so you have to store the relevant information about your records in another list or in a dataset. As long as your listbox is not sorted (that means, listbox and objects list are in same order) you can do something like that:

C#
for(int i=0; i<listbox.Count; i++)
{
  // for another list
  // id = yourObjectList[i].id;

  //for a dataset
  id = yourDataset.Rows[i][indexOfId];

  execSql("delete from yourTable where yourId="+id);
}


P.S.: As your list is a list of unique filenames, you can try this:

string files = string.Empty;
string separator = "";
for each(item in listbox.items)
{
  files += separator + "'" + item + '"';
  separator = ", ";
}

sql = "delete from yourTable where filename in ("+files+")";
execSql(sql);
 
Share this answer
 
v2
Comments
bachasafyan 24-Jan-11 6:02am    
@Corinna J , i am trying it and will let you know
thnx for the post and reading.
bachasafyan 24-Jan-11 6:10am    
I am using here Oledb not dataset rows.
I have just one list in which i have all the files names.
Now what i want to do is to compare it to the name column row by row, and if a row value do not match the listbox name then, that record should be deleted.
If you want to delete selected listbox items
foreach (ListItem item in ListBox1.Items)
                {
                    if (item.Selected == true && item == valuefromdb)
                    {                        
                       //code to delete from db 
                    }
                }

If you want to delete matching items in listbox from db then

foreach (ListItem item in ListBox1.Items)
                {
                  
                    if (item == valuefromdb)
                    {
                        //code to delete from db
                    }
                }
 
Share this answer
 
Comments
bachasafyan 24-Jan-11 6:01am    
@mkgoud, i am trying it and will let you know
thnx for the post and reading.
bachasafyan 24-Jan-11 6:11am    
Now what i want to do is to compare it to the name column row by row, and if a row value do not match the listbox name then, that record should be deleted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900