0

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();
}
5
Contributors
10
Replies
13
Views
8 Years
Discussion Span
Last Post by sayeevani
0
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();
}
0
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();
0

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 ...

0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.