Reputation: 15
I have listview with four columns. I have created table in the database with four columns. But i can not save these items with this code. please help me.
foreach (ListViewItem l in listView1.Items)
{
cn = new SqlConnection(@"Data Source=DESKTOP-1F1TUP2\SQLEXPRESS;Initial Catalog=savedata;Integrated Security=True");
cn.Open();
cmd = new SqlCommand("Insert Into listview(item,quantity,unit_price,sub_total) VALUES (@item,@quantity,@unit_price,@sub_total)", cn);
cmd.Connection = cn;
cmd.Parameters.AddWithValue("item", l.SubItems[0]);
cmd.Parameters.AddWithValue("quantity", l.SubItems[1]);
cmd.Parameters.AddWithValue("unit_price", l.SubItems[2]);
cmd.Parameters.AddWithValue("sub_total", l.SubItems[3]);
cmd.ExecuteNonQuery();
}
Upvotes: 0
Views: 47
Reputation: 484
the parameter needs to be added as "@item"
cmd.Parameters.AddWithValue("@item", l.SubItems[0]);
you can try this also
cmd.Parameters.Add(new SqlParameter("@item", l.SubItems[0]));
Upvotes: 1