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

C#
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();

           }
Posted

1 solution

Have if condition in the beginning of for loop as below

C#
foreach (ListViewItem li in listView1.Items)
           {
               if (li.SubItems[0].Text=="Booked")
               {
               OleDbCommand cmd = new OleDbCommand("insert into bookings (code,client_name)values('" + li.SubItems[0].Text + "','" + li.SubItems[1].Text + "');", con);
               cmd.ExecuteNonQuery();
             }
           }
 
Share this answer
 

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