c# - SQL Update - Not updating multiple rows in a for loop -
i have datagridview(dgv) linked sqlite database. want update info on dgv. so, have context menu lets me change 1 column , update db well. want have ability select multiple rows , edit well. ex: if select 5 rows , change type alarms errors ; change reflected in dgv , when database , change isnt reflected. 1 row updated.
my code snippet below
foreach (datagridviewrow r in datagridview1.selectedrows) { sqlitetransaction sqlitetrans = connection.begintransaction(); sqlitecommand cmd = connection.createcommand(); messagebox.show(r.tostring()); if (r.cells["typedatagridviewtextboxcolumn"].value.tostring().contains("#") == false) { r.cells["typedatagridviewtextboxcolumn"].value = r.cells["typedatagridviewtextboxcolumn"].value.tostring() + " # " + max; } else { r.cells["typedatagridviewtextboxcolumn"].value = r.cells["typedatagridviewtextboxcolumn"].value.tostring().substring(0, r.cells["typedatagridviewtextboxcolumn"].value.tostring().indexof("#")) + "# " + max; } string querytext = "update logdatabase set type = \"" + r.cells["typedatagridviewtextboxcolumn"].value + "\" hashkey = \"" + r.cells["hashkeydatagridviewtextboxcolumn"].value.tostring() + "\""; cmd.commandtext = querytext; cmd.executenonquery(); sqlitetrans.commit(); }
i dont have experience sql. so, im not sure if wrong how ive updated database!
what have edit make sure rows updated in db well?!
help appreciated.
edit: tried checking query before sent.
when try editing multiple rows in dgv without sorting dgv under column works , updates rows simultaneously... when try sort them based on "type" , edit rows, same query passed ! :| (hash key doesnt change)
its like, 1 row keeps moving list of rows , row r in loop.
edit 2: problem rows of dgv everytime sort dgv , try edit fields, queries have hashkey values different once selected. row ids changed after 1 update. looks dgv automatically sorts once 1 row updated !
is there way disable this???!
it looks weren't increasing max counter.
foreach (datagridviewrow row in datagridview1.selectedrows) { using (sqlitetransaction sqlitetrans = connection.begintransaction()) { sqlitecommand cmd = connection.createcommand(); messagebox.show(row.tostring()); var txtvalue = row.cells["typedatagridviewtextboxcolumn"].value; if (txtvalue.contains("#")) { txtvalue = string.format("{0} # {1}", txtvalue, max); }else { txtvalue = txtvalue.substring(0, txtvalue.indexof("#")) + "# " + max.tostring(); } string querytext = "update logdatabase set type = @type hashkey = @key"; //create sqlparameters here! cmd.commandtext = querytext; cmd.executenonquery(); sqlitetrans.commit(); max++; //you weren't increasing counter!! } }
Comments
Post a Comment