mind controll

mind controll

  • 1.9k
  • 131
  • 55.4k

multi sql table update with listview items

Oct 17 2015 8:39 AM

i have sql table 1 that contains itemcode - itemcount - tablename
and my windows form1 contains listview1 and button1
what i'm trying to do is that when i click on button1 all listview1 SubItems[1] values in the column to update from tables names in SubItems[2] values where itemcode values are SubItems[0]
i tried the following code but it did what it should do but for the first row only not the rest:

foreach (ListViewItem item in listView1.Items)

{

item.Selected = true;

}

if (cn.State == ConnectionState.Closed) cn.Open();

cm = new SqlCommand();

cm.Connection = cn;

ListViewItem lvi1 = listView1.SelectedItems[0];

string tableName = lvi1.SubItems[2].Text;

string itemcode1 = lvi1.SubItems[0].Text;

string itemcode2 = lvi1.SubItems[1].Text;

string sql = "UPDATE " + tableName + " SET [itemcount]= " + itemcode2 + " WHERE [itemcode]= " + itemcode1 + " AND [itemcount] IS NOT NULL";

cm.CommandText = sql;

cm.ExecuteNonQuery();