C# .NET - how to filter list view records - Asked By Priyanka on 02-Jun-13 01:33 PM

 am using below code to insert records from list view to database.. it is working fine it inserts all the records from list view to database. but how can i filter records using foreach loop. actually i want to insert only that records which have status 'Booked' in list view's fifth column. Status column is in fifth number of list view. and it is not in the database. Hope you understand my question.
 

Thanks in advance..



foreach (ListViewItem li in listView1.Items)
           {
               OleDbCommand cmd = new OleDbCommand("insert into bookings (code,client_name)values('" + li.SubItems[0].Text + "','" + li.SubItems[1].Text + "');", con);
               cmd.ExecuteNonQuery();
 
           }
Reena Jain replied to Priyanka on 03-Jun-13 02:33 AM
Hi,

Just use below code

foreach (ListViewItem li in listView1.Items)
       {
if(li.SubItems[4].Text.ToLower()=="booked")
{
         OleDbCommand cmd = new OleDbCommand("insert into bookings (code,client_name)values('" + li.SubItems[0].Text + "','" + li.SubItems[1].Text + "');", con);
         cmd.ExecuteNonQuery();
 }
       }

Hope this will solve your problem