c# - DataReader open on a connection in a while loop despite being specifically closed -


there open datareader associated command must closed first.

okay. so. lot of database reading in this, enough justify generic function simplify it. i've changed function no longer closes connections when called active one, since it's 1 line either way. in doing so, found bug. on second iteration of while loop, dbread function error when trying execute, complaining per above error. however. @ end of each iteration, there's reader.close() call no condition.

now... i've done looking around this, , discovered using using these... honest, i'm not sure how here (i figure out enough, guess), moreover, don't know if fix problem.

every other question read not closing. whereas in case, think i'm calling 1 close every open, on readers, , reusing connection after that. how wrong i?

        // find unprocessed gifts in inhouse table.         sql = "select * toregift processed = 0";         readcommand.connection = readconnect;         readcommand.commandtext = sql;         dbreread = readcommand.executereader();         while (dbreread.read())  // loop through them.         {             rowid = convert.toint32(dbreread["id"].tostring());              // lookup re constituentid toreconstituent table.             string toreconstituent_id = dbreread["toreconstituent_id"].tostring();             reader = functionsclass.dbread(vars.catalog, "select constituent_id toreconstituent id = " + toreconstituent_id.tostring(), ref connection);             reader.read();             string constituentid = reader["constituent_id"].tostring();             bool userg = false;             try             {                 userg = convert.toboolean(dbreread["use_rg"]);             }             catch (exception ex) { }             ;              if (!userg)             {                 if (dbreread["typeofgift"].tostring() == "recgiftpc")                 {                     userg = true;                 }             }              addsinglegift(constituentid, userg, rowid);              sql = "select constituent_id toreconstituent id = " + toreconstituent_id;             dbrecommand.commandtext = sql;             reader = dbrecommand.executereader();             while (reader.read())             {                 constituentid = reader["constituent_id"].tostring();             }             reader.close();          }         dbreread.close();  --  public static sqldatareader dbread(string db, string sql, ref sqlconnection connection) {     sqldatareader functionreturnvalue = null;     // !! important !!     // initialise , open connection needed.     // caller responsible closing connection     if(connection==null)     {         connection = new sqlconnection();     }     if(connection.connectionstring=="")     {         connection.connectionstring = "data source = redev;initial catalog = " + db + ";integrated security = true";     }     try     {         connection.open();     }     catch (exception ex)     {         if(ex.message!="the connection not closed. connection's current state open.")         {             throw ex;         }         // else continue, because if that's error, doesn't matter, move on.     }     sqlcommand dbcommand = new sqlcommand();      dbcommand.connection = connection;     dbcommand.commandtype = system.data.commandtype.text;     dbcommand.commandtext = sql;     dbcommand.commandtimeout = 0;      try {         functionreturnvalue = dbcommand.executereader();         return functionreturnvalue;     } catch (exception ex) {         //frmmain.batcherror = true;         system.windows.forms.messagebox.show("error reading database. can't continue, sorry. debug information below:" + vars.vbcrlf + vars.vbcrlf + ex.message + vars.vbcrlf + sql + vars.vbcrlf + vars.vbcrlf + connection.connectionstring);         connection.close();         system.environment.exit(0);     }      return functionreturnvalue;     // cleanup     //dbcommand = nothing  } 


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -