c# - There is already an open DataReader associated with this Command which must be closed first. can't I open new data reader in existing data reader -


can't open new data reader in existing data reader?? plzz me. i'm new c#

string statement11 = "select planning allow nplid=(select max(nplid) allow)";  sqlcommand mycommand11 = new sqlcommand(statement11, con1); sqldatareader plan2 = mycommand11.executereader(); while(plan2.read())  if (!plan2.isdbnull(0) && "ok" == plan2.getstring(0)) {     string statement99 = "select dropplan nplqanew nplid=(select max(nplid) allow)";     sqldatareader myreader1 = null;     sqlcommand mycommand114 = new sqlcommand(statement99, con1);     sqldatareader plandrop = mycommand114.executereader();     while (plandrop.read())         if (plandrop.isdbnull(0) && plandrop.getstring(0) == "red")         {              lblplan1.backcolor = system.drawing.color.red;         }         else if (plandrop.isdbnull(0) && "amber" == plandrop.getstring(0))         {              lblplan1.backcolor = system.drawing.color.orange;         }         else if (plandrop.isdbnull(0) && "green" == plandrop.getstring(0))         {             lblplan1.backcolor = system.drawing.color.green;         }     plandrop.close();       this.lblplan1.visible = true;    }  plan2.close(); 

by default, sql server client not let open 2 simultaneous queries on same connection. if in process of reading results of 1 data reader, example, cannot use same connection start reading second. and, way sql server connection pooling works, asking "new" connection not guaranteed work either.

you have couple of options on how fix this. first refactor code eliminate nested sql execute calls; example, load results of first query memory before loop through , process them.

an easier answer enable "mars" - multiple active recordsets - on connection. done setting "mars connection=true option on connection string turn feature on. generally pretty safe do, , it's off default preserve pre-2005 behavior old applications, linked article give guidelines.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -