Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I was wondering if how can I add/ insert data from a Listview to a local database in c#? Thanks.
Posted
Comments
nilesh sawardekar 28-Jul-14 1:25am    
Do you want to insert each row of listview in database?
Jenni Dumancas 29-Jul-14 2:18am    
Yes.
_Amy 28-Jul-14 13:00pm    
What do you mean by Local Database? Are you populating ListView from any other resource?

C#
for (int i = 0; i < lvProductInfo.Items.Count; i++)
    {
        ProductCode = Convert.ToInt32(lvProductInfo.Items[i].SubItems[1].Text);
        ProductQuantity = Convert.ToInt32(lvProductInfo.Items[i].SubItems[2].Text);
        ProductPrice = Convert.ToInt32(lvProductInfo.Items[i].SubItems[3].Text);
        totalPrice = Convert.ToInt32(lvProductInfo.Items[i].SubItems[4].Text);


        sql = "";
        sql = "INSERT INTO PurchaseLog (ProductCode,ProductQuantity,ProductPrice,TotalPrice)"
            + " VALUES ('" + ProductCode + "','" + ProductQuantity + "','" + ProductPrice + "','" + totalPrice + "')";

        clsConnection clsCn = new clsConnection();
        SqlConnection cn = null;
        SqlCommand cmd = new SqlCommand();

        clsCn.fnc_ConnectToDB(ref cn);

        cmd.Connection = cn;
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery();


        this.Close();
    }

}
 
Share this answer
 
v2
Go through ListView Control[^]
 
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