c# - Close .netslclient data provider within function -


i have listview bound function called getamountstyles(). when function fired creates 44 sql connections. no matter try close sql connection, won't close. , how can close these connections?

 protected string getamountstyle() {     //makes sql connection      sqlconnection testdbconn = new sqlconnection(configurationmanager.connectionstrings["testdbconn"].connectionstring);     //sqlcommand cmd = new sqlcommand();    testdbconn.open();      system.data.sqlclient.sqlcommand currallsessions = new system.data.sqlclient.sqlcommand("select username tenhrslogins union select username permlogins ", testdbconn);     sqldatareader dr = currallsessions.executereader();     string login = convert.tostring(eval("login"));         while (dr.read())     {         //if (login.contains(dr.getvalue(0).tostring()) == true)         if (login.contains(dr.getvalue(0).tostring()) == true)         {              dr.dispose();             return "background-color: #ffc6c6;";            }         else         {            }       }     dr.dispose();     testdbconn.close();     return "testdbconn.close();"; } 

you're returning

return "background-color: #ffc6c6;"; 

before close connection. rule of thumb use using, no need explicit dispose or close. this:

using(var testdbconn = new sqlconnection(...)) {    testdbconn.open();    // whatever connection, close automatically    using(var dr = currallsessions.executereader()) {      // whatever data reader    } } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -