Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
how to save listview items & subitems in .db file ( data base) and with update current database and load it

or save/update/load in xml.

I serach in google and only find save listview With txt file.
Posted
Updated 17-Sep-14 22:34pm
v2
Comments
BillWoodruff 18-Sep-14 4:53am    
What have you tried so far ? Show us some code.

Well you don't show how many columns you have or what type they are, so I can only give general advice.
Also I know nothing about your database structure.

One way you can solve this problem is to create a DataTable that reflects the structure in your database.
C#
DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(double));


Then you go through the list view in a loop and add rows to your data table.
C#
foreach (ListViewItem item in listView1.Items)
{
    DataRow drNew = dt.NewRow();
    drNew["Column1"] = int.Parse(item.SubItems[0].Text);
    drNew["Column2"] = item.SubItems[0].Text;
    drNew["Column3"] = double.Parse(item.SubItems[0].Text);
    dt.Rows.Add(drNew);
}
dt.AcceptChanges();


If you want to save the data as XML, you just use this code line:
C#
dt.WriteXml(@"C:\SomeFolder\SomeFileName.xml");


This is one simplistic way to save the data into the database
C#
string sql = "INSERT INTO table_name (Column1, Column2, Column3) VALUES (@col1, @col2, @col3)";
using (SqlConnection conn = new SqlConnection(connectionString))
{
   conn.Open();
   foreach (DataRow dr in dt.Rows)
   {
       SqlCommand cmd = conn.CreateCommand();
       cmd.CommandText = sql;
       cmd.Parameters.AddWithValue("@col1", dr["Column1"]);
       cmd.Parameters.AddWithValue("@col2", dr["Column2"]);
       cmd.Parameters.AddWithValue("@col3", dr["Column3"]);
       cmd.ExecuteNonQuery();
   }
}


There are many other ways to do this.
 
Share this answer
 
hai you can write one data access layer method to save data into data base and and add reference this to your current project. then call that dal method whenever you want to save list items.
 
Share this answer
 
Comments
George Jonsson 18-Sep-14 5:27am    
I think he wants to know how to write this method.
Member 10267630 19-Sep-14 2:42am    
yes i wants to know how to write this method

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