How to insert particular or selected record of listview

I have a listview in my form which contains three columns "Name","Age" and "Class". All the column contains some data. Now, i want to insert the records from list view into database where class = 5th. How to do this?
Posted by Prince Mahajan 3/23/2014 5:38:55 PM
Total Answers : 2
Answer 1
Use the below code for your requirement. Just replace the index of your class column.
 OleDbConnection con = new OleDbConnection(c.connectionstring);
foreach (ListViewItem li in listView1.Items)
if (li.SubItems[2].Text == "5tn")
OleDbCommand cmd = new OleDbCommand("insert into table_name
(name,age,class)values('" + li.SubItems[0].Text + "'
,'" + li.SubItems[1].Text + "','" + li.SubItems[2].Text + "');", con);

Posted by StepCoder Team 3/25/2014 1:05:23 PM
Answer 2
Just use the if clause for validating the value first, after that insert the record.. Try the above code.
Posted by Andrew Dvorkin 3/26/2014 5:15:51 PM
Post your answer
Sign in to your account
Your Email : 
Password :   
Forgot your password
Don't have StepCoder account ?
Create a new account
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
Answer these questions! If you know the answer.