How to save listview data to database?

Not work code:

private void Insertbutton_Click(object sender, EventArgs e)
{
string strConnection = "Data Source=OCH-ERDENE\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True; Pooling=False";
SQLiteConnection conn = new SQLiteConnection(strConnection);
conn.Open();
for (int cnt = 0; cnt <= lsvXML.Items.Count; cnt++)
{
string query = "INSERT INTO DataObject VALUES('" + lsvXML.Items[cnt].Text + "')";
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}
conn.Close();
}
private void Insertbutton_Click(object sender, EventArgs e)
{
string strConnection = "Data Source=OCH-ERDENE\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True; Pooling=False";
SQLiteConnection conn = new SQLiteConnection(strConnection);
conn.Open();
for (int cnt = 0; cnt <= lsvXML.Items.Count; cnt++)
{
string query = "INSERT INTO DataObject VALUES('" + lsvXML.Items[cnt].Text + "')";
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.ExecuteNonQuery();// [B]Insert Error: Column name or number of supplied values does not match table definition.[/B]
}
conn.Close();
}
string strConnection = "Data Source=TSERENDASH\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True;Pooling=False";
            SqlConnection conn = new SqlConnection(strConnection);
            conn.Open();
            for (int cnt = 0; cnt <= lsvXML.Items.Count; cnt++)
            {
                string query = "INSERT INTO DataObject VALUES('" + lsvXML.Items[cnt].Text + "')";
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();//[B]Insert Error: Column name or number of supplied values does not match table definition.[/B]
            }
            conn.Close();

The basic problem is that your XML file has either more or less columns than your Table, i would recommend writing the table on the button click...else add some null values ...

Or telling it which of the columns you are inserting eg, it has an auto increment field/ID etc

put cnt <= lsvXML.Items.Count-1 and assign the values for insert like

insert into Roomtype(roomtype,Max_Adults,Max_Child)values('" + listView1.Items[cnt].Text + "','" + listView1.Items[cnt].SubItems[1].Text + "','" + listView1.Items[cnt].SubItems[2].Text + "')

This article has been dead for over six months. Start a new discussion instead.