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();
}

So, in what way does it not work? Whats the error? what debugging did you do?

Catalog=Database your database called database?? Give us the error, to help you :)

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

Why he doesn't answer us and just write a code?!

He kinda did, he added the error as a comment in the code..

So, please give us your table structure....

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 + "')

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.