Nawoda Wijebandara
Nawoda Wijebandara

Reputation: 15

I can not save listview items to a database

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

        }

enter image description here

Error

Upvotes: 0

Views: 47

Answers (1)

Jitan Gupta
Jitan Gupta

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

Related Questions