c# - Why is DataTable copy getting truncated when the original is being truncated? -


i trying preserve copy of original datatable while trying truncate original purpose. when truncate original, copy gets truncated.

using (sqlcommand cmd = new sqlcommand(sqlselect, conn)) {     //set time minute , half, should not take longer that!     cmd.commandtimeout = 90;     datatable dt = new datatable();     datatable copydt = new datatable();      //check state of database connection. if closed, open it.     if (conn.state != connectionstate.open)         conn.open();      dt.load(cmd.executereader());     copydt = dt.copy();      if (session["servicerequestsearch"] != null)     {         ((datatable)session["servicerequestsearch"]).clear();         ((datatable)session["servicerequestsearch"]).dispose();         session.remove("servicerequestsearch");     }      session["servicerequestsearch"] = copydt;      if (dt.rows.count > take)         (int = take; < dt.rows.count; i++)             dt.rows[i].delete();      dt.acceptchanges();     textbox.text = dt.rows.count + "";     textbox2.text = ((datatable)session["servicerequestsearch"]).rows.count + "";        //close sql data connection if still open.     if (conn.state != connectionstate.closed)         conn.close(); } 

they both show same number row count, though copy should lot higher.

i don't know or setting take to, right before loop, checking if row count greater take , setting i = take in loop. if take same number row count, not have deleted rows.

that looks culprit , advise step through code , verify dt.rows[i].delete(); triggers breakpoint.

your check if dt.rows.count > take seems little redundant too. loop limit correct number of rows need deleted. depending on take set to, may failing check.

edit: looking @ code more, think realized mistake made

you want instead:

if (dt.rows.count > take)     (int = dt.rows.count - 1; > take; i--)         dt.rows[i].delete(); 

not sure if take supposed delete first items or not, on right track being able delete rows.

edit 2: have updated loop after caught items should removed in reverse. @yuriygalanter.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -