Sunday, 15 June 2014

c# - Reference previous object created in loop to update SQL -



c# - Reference previous object created in loop to update SQL -

i have loop sets form, next code (in form load event). displays checkbox persons name. checks checkbox if bit field 1.

int xaxischeckbox = 130; int yaxischeckbox = 30; (int = 0; < selectds.tables[0].rows.count; i++) { this.mycheckbox = new checkbox(); mycheckbox.location = new point(xaxischeckbox, yaxischeckbox); mycheckbox.size = new size(120, 20); mycheckbox.text = selectds.tables[0].rows[i]["fullname"].tostring(); mycheckbox.checked = (bool)selectds.tables[0].rows[i]["inoperation"]; yaxischeckbox = yaxischeckbox + 80; }

later on in code (for save button click event), runs same select load of updates set inoperation field true/false depending on tick. resets operationorder if beingness added operation.

for (int = 0; < selectdataset.tables[0].rows.count; i++) { userid = (int)selectdataset.tables[0].rows[i]["userid"]; if (mycheckbox.checked) { connection.runupdate("update users set inoperation = 1, operationorder = case when operationorder = 1 1 else case when inoperation=1 operationorder else (select count(*)+1 users inoperation=1 , operationorder > 0) end end userid=" + userid); connection.runupdate("update users set operationorder = case when operationorder = 0 (select count(*) users inoperation=1) else operationorder end inoperation=1"); } else { connection.runupdate("update users set inoperation = 0, operationorder = 0 userid=" + userid); connection.runupdate("update users set operationorder = case when operationorder -1 = 0 (select count(*) users inoperation=1) else operationorder -1 end inoperation=1"); } }

the problem updates every single row based upon lastly object created (e.g. if 5 rows, bottom checkbox count running sql, , applies of them). how can update every single row, there way can reference each object create rather lastly 1 created?

update: here of new code causing errors. public partial class selectusers : form { public int userid; public list myboxes;

public selectusers() { initializecomponent(); } private void selectusers_load(object sender, eventargs e) { dataset ds = myconnection.runselect(new dataset(), "the select"); int xaxischeckbox = 40; int yaxischeckbox = 50; myboxes = new list<checkbox>(); (int = 0; < ds.tables[0].rows.count; i++) { this.mycheckbox = new checkbox(); mycheckbox.location = new point(xaxischeckbox, yaxischeckbox); mycheckbox.size = new size(120, 20); mycheckbox.text = ds.tables[0].rows[i]["fullname"].tostring(); mycheckbox.checked = (bool)ds.tables[0].rows[i]["inoperation"]; yaxischeckbox = yaxischeckbox + 80; myboxes.add(mycheckbox); } } private void savebtn_click(object sender, eventargs e) { dataset ds = myconnection.runselect(new dataset(), "the select"); (int = 0; < ds.tables[0].rows.count; i++) { userid = (int)ds.tables[0].rows[i]["userid"]; if (myboxes[i].checked) { myconnection.runupdate("update users set inoperation = 1, operationorder = case when operationorder = 1 1 else case when inoperation=1 operationorder else (select count(*)+1 users inoperation=1 , operationorder > 0) end end userid=" + userid); myconnection.runupdate("update users set operationorder = case when operationorder = 0 (select count(*) users inoperation=1) else operationorder end inoperation=1"); } else { myconnection.runupdate("update users set inoperation = 0, operationorder = 0 userid=" + userid); myconnection.runupdate("update users set operationorder = case when operationorder -1 = 0 (select count(*) users inoperation=1) else operationorder -1 end inoperation=1"); } } }

you should maintain array of checkboxes rather individual checkbox

int xaxischeckbox = 130; int yaxischeckbox = 30; list<checkbox> myboxes = new list<checkbox>(); (int = 0; < selectds.tables[0].rows.count; i++) { this.mycheckbox = new checkbox(); mycheckbox.location = new point(xaxischeckbox, yaxischeckbox); mycheckbox.size = new size(120, 20); mycheckbox.text = selectds.tables[0].rows[i]["fullname"].tostring(); mycheckbox.checked = (bool)selectds.tables[0].rows[i]["inoperation"]; yaxischeckbox = yaxischeckbox + 80; myboxes.add(mycheckbox); }

and later ir loop:

for (int = 0; < selectdataset.tables[0].rows.count; i++) { userid = (int)selectdataset.tables[0].rows[i]["userid"]; if (myboxes[i].checked) { connection.runupdate("update users set inoperation = 1, operationorder = case when operationorder = 1 1 else case when inoperation=1 operationorder else (select count(*)+1 users inoperation=1 , operationorder > 0) end end userid=" + userid); connection.runupdate("update users set operationorder = case when operationorder = 0 (select count(*) users inoperation=1) else operationorder end inoperation=1"); } else { connection.runupdate("update users set inoperation = 0, operationorder = 0 userid=" + userid); connection.runupdate("update users set operationorder = case when operationorder -1 = 0 (select count(*) users inoperation=1) else operationorder -1 end inoperation=1"); } }

that should it.

it goes without saying, executing sql statements directy in form not idea, thats story

c# sql object checkbox

No comments:

Post a Comment