Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have Wpf LisView As show in the Preview

Now, i'm having Challenge saving the items/values Of the ListView into the Database, This is the code i used for saving the Items.

C#
SelectedCategory sc = new SelectedCategory();
SQLiteConnection con = new SQLiteConnection("  Data Source=database.sqlite; Version=3; Compress=True; ");
con.Open();
string query = " INSERT INTO income_details (name, amount)  VALUES (@1, @2) ";
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", ListView_Selected_Category.Items[0]));
     cmd.Parameters.Add(new SQLiteParameter("@2", ListView_Selected_Category.Items[0]));
     cmd.ExecuteNonQuery();
}
MessageBox.Show("Saved");
con.Close(); 


Cause i cant get the ListView Column.

Please i really need help, Thanks In advance.

What I have tried:

C#
SelectedCategory sc = new SelectedCategory();
SQLiteConnection con = new SQLiteConnection("  Data Source=database.sqlite; Version=3; Compress=True; ");
con.Open();
string query = " INSERT INTO income_details (name, amount)  VALUES (@1, @2) ";
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", ListView_Selected_Category.Items[0]));
     cmd.Parameters.Add(new SQLiteParameter("@2", ListView_Selected_Category.Items[0]));
     cmd.ExecuteNonQuery();
}
MessageBox.Show("Saved");
con.Close();
Posted
Updated 26-Apr-16 18:15pm
v2
Comments
VR Karthikeyan 26-Apr-16 23:52pm    
show your code for assigning items to ListView? How do you assign values to ListView?

1 solution

Hi, just convert your ListView Items to object of its type, and now you can access Category Name, Amount, Discount properties from converted object. just refer the code below,
C#
for (int i = 0; i < ListView_Selected_Category.Items.Count; i++)
{
     YourCategoryClass CategoryObject = (YourCategoryClass)ListView_Selected_Category.Items[i];
     SQLiteCommand cmd = new SQLiteCommand(query, con);
     cmd.Parameters.Add(new SQLiteParameter("@1", CategoryObject.CategoryName));
     cmd.Parameters.Add(new SQLiteParameter("@2", CategoryObject.Amount));
     cmd.ExecuteNonQuery();
}
 
Share this answer
 
Comments
Member 12227245 13-Jul-16 3:55am    
Thank you it was really helpful

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