c# - I can open SqlConnection once, but I can't do it again -
my program runs, , connects database successfully. can run queries , works fine. however, cant seem disconnect , reconnect database. output says: "a first chance exception of type 'system.invalidoperationexception' occurred in system.data.dll" here connection method:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.threading; using system.data.sqlclient; namespace windowsformsapplication1 { class tcninteraction { static string connectionstring = "server=localhost\\sqlexpress;database=tcn;trusted_connection=true"; button thebutton; textbox thetextbox; bool currentlyconnectedtoadatabase = false; sqlconnection cnn = new sqlconnection(connectionstring); /* *uses existing sql connection string , attempts establish connection */ public bool connecttodatabase(button inputbutton, textbox inputdatabasebox) { thebutton = inputbutton; try { console.writeline("attempting open connection..."); if (cnn.state != connectionstate.open) { console.writeline("about open connection"); cnn.open(); } console.write("i opened connection!"); currentlyconnectedtoadatabase = true; displayrowsofdatabase(inputdatabasebox); thebutton.text = "connected"; return true; } catch (exception ex) { messagebox.show("cannot open connection!"); return false; } { cnn.close(); } } private void displayrowsofdatabase(textbox inputdatabasebox) { thetextbox = inputdatabasebox; using (cnn) { sqlcommand command = new sqlcommand("select somenumber,sometext, anothernumber tcn.demo;", cnn); sqldatareader reader = command.executereader(); if (reader.hasrows) { while (reader.read()) { string printstring = int.parse(reader["somenumber"].tostring()) + " " + reader["sometext"].tostring() + " " + int.parse(reader["anothernumber"].tostring()) + environment.newline; thetextbox.text += printstring; } } else { console.writeline("no rows found."); } reader.close(); } cnn.close(); } public void disconnectfromdatabase() { messagebox.show("disconnected database"); cnn.close(); thebutton.text = "connect database"; //thetextbox.text = ""; currentlyconnectedtoadatabase = false; } } }
your try/catch
block not closing connection. give responsibility close connection finally
block. close connection if exception occurs in program. try following...
public bool connecttodatabase(button inputbutton, textbox inputdatabasebox) { thebutton = inputbutton; try { console.writeline("attempting open connection..."); //this stuck second time if (cnn.state != connectionstate.open) { cnn.open(); } console.write("i opened connection!"); currentlyconnectedtoadatabase = true; displayrowsofdatabase(inputdatabasebox); thebutton.text = "connected"; return true; } catch (exception ex) { messagebox.show("cannot open connection!"); } { cnn.close(); } } public void disconnectfromdatabase() { messagebox.show("disconnected database"); if(cnn.state == connectionstate.open) { cnn.close(); } thebutton.text = "connect database"; //thetextbox.text = ""; currentlyconnectedtoadatabase = false; }
Comments
Post a Comment