sql server - c# windows forms logout -


how make logout button in c# windows forms closes mssql connection.

i have login form sends username , password 1 form another. connection string on form2 placed under public partial class , looks this:

public static sqlconnection con = new sqlconnection(@"data source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\sqlexpress;database=ha;persist security info=false; uid='" + globalvariables.user + "' ; pwd='" + globalvariables.psw + "'"); sqlcommand mysqlcmd = con.createcommand(); 

i've created logout button on form2 goes first login form, doesn't seem close sqlconnection. can press login button again without entering user , password , through form2 again.

so first time have enter username , password , second time dont need to.

the code use logout button is:

con.close(); this.close(); form fmlogind = new logind(); fmlogind.show();  

here whole code on second form:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sqlclient; using system.io;   namespace windowsformsapplication1 {       public partial class form1 : form     {               public static             sqlconnection con = new sqlconnection(@"data source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\sqlexpress;database=ha;persist security info=false; uid='" + globalvariables.user + "' ; pwd='" + globalvariables.psw + "'");             sqlcommand mysqlcmd = con.createcommand();              public form1()         {             initializecomponent();             this.text ="easyaud";          }           public void form1_load(object sender, eventargs e)         {                    easyaudtoolstripmenuitem.text = globalvariables.user;              combobox4.items.add("1");             combobox4.items.add("2");              mysqlcmd.commandtext = "select distinct firma app";             con.open();             sqldatareader firmareader = mysqlcmd.executereader();              while (firmareader.read())             {                 combobox1.items.add(firmareader["firma"]);             }              firmareader.close();             con.close();              mysqlcmd.commandtext = "select distinct type app";             con.open();             sqldatareader typereader = mysqlcmd.executereader();              while (typereader.read())             {                 combobox2.items.add(typereader["type"]);             }              typereader.close();             con.close();           }            private void combobox1_selectedindexchanged_1(object sender, eventargs e)         {                  listbox1.items.clear();                 combobox3.items.clear();                if (combobox1.text != "" && combobox2.text == "" && combobox3.text == "")             {                  string firma = combobox1.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text == "" && combobox2.text != "" && combobox3.text == "")             {                   string type = combobox2.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app type = '" + type + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app type = '" + type + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text == "" && combobox2.text == "" && combobox3.text != "")             {                   string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app farve = '" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app farve = '" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();              }              if (combobox1.text != "" && combobox2.text != "" && combobox3.text != "")             {                  string firma = combobox1.selecteditem.tostring();                 string type = combobox2.selecteditem.tostring();                 string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , type ='" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , type ='" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();              }              if (combobox1.text != "" && combobox2.text != "" && combobox3.text == "")             {                  string firma = combobox1.selecteditem.tostring();                 string type = combobox2.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , type ='" + type + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , type ='" + type + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text != "" && combobox2.text == "" && combobox3.text != "")             {                  string firma = combobox1.selecteditem.tostring();                  string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();                }               if (combobox1.text == "" && combobox2.text != "" && combobox3.text != "")             {                   string type = combobox2.selecteditem.tostring();                 string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app type = '" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app type = '" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();                }         }          private void combobox2_selectedindexchanged(object sender, eventargs e)         {              listbox1.items.clear();             combobox3.items.clear();              if (combobox1.text != "" && combobox2.text == "" && combobox3.text == "")             {                  string firma = combobox1.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();                }              if (combobox1.text == "" && combobox2.text != "" && combobox3.text == "")             {                   string type = combobox2.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app type = '" + type + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app type = '" + type + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text == "" && combobox2.text == "" && combobox3.text != "")             {                   string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app farve = '" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app farve = '" + farve + "'";                                con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();              }              if (combobox1.text != "" && combobox2.text != "" && combobox3.text != "")             {                  string firma = combobox1.selecteditem.tostring();                 string type = combobox2.selecteditem.tostring();                 string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , type ='" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , type ='" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text != "" && combobox2.text != "" && combobox3.text == "")             {                  string firma = combobox1.selecteditem.tostring();                 string type = combobox2.selecteditem.tostring();                   mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , type ='" + type + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                  mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , type ='" + type + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }              if (combobox1.text != "" && combobox2.text == "" && combobox3.text != "")             {                  string firma = combobox1.selecteditem.tostring();                  string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app firma = '" + firma + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app firma = '" + firma + "' , farve ='" + farve + "'";                                con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();               }               if (combobox1.text == "" && combobox2.text != "" && combobox3.text != "")             {                   string type = combobox2.selecteditem.tostring();                 string farve = combobox3.selecteditem.tostring();                  mysqlcmd.commandtext = "select distinct ha app type = '" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader firmareader = mysqlcmd.executereader();                  while (firmareader.read())                 {                     listbox1.items.add(firmareader["ha"]);                 }                  firmareader.close();                 con.close();                   mysqlcmd.commandtext = "select distinct farve app type = '" + type + "' , farve ='" + farve + "'";                 con.open();                 sqldatareader farvereader = mysqlcmd.executereader();                  while (farvereader.read())                 {                     combobox3.items.add(farvereader["farve"]);                 }                  farvereader.close();                 con.close();              }          }          private void combobox3_selectedindexchanged(object sender, eventargs e)         {          }          private void listbox1_selectedindexchanged(object sender, eventargs e)         {             combobox3.items.clear();             string ha = listbox1.selecteditem.tostring();              mysqlcmd.commandtext = "select distinct farve app ha = '" + ha + "'";             con.open();             sqldatareader farvereader = mysqlcmd.executereader();              while (farvereader.read())             {                 combobox3.items.add(farvereader["farve"]);             }              farvereader.close();             con.close();         }          private void combobox3_selectedindexchanged_1(object sender, eventargs e)         {          }          private void combobox4_selectedindexchanged(object sender, eventargs e)         {          }          private void button1_click(object sender, eventargs e)         {             listbox1.items.clear();             combobox1.selectedindex = -1;             combobox2.selectedindex = -1;             combobox3.selectedindex = -1;             combobox4.selectedindex = -1;         }          private void button2_click(object sender, eventargs e)         {              if (combobox4.text == "" || combobox3.text == "" || listbox1.text == "")             {                  messagebox.show("ha-valg blev ikke gemt!" + "\n" + "felterne: antal, ha og farve skal udfyldes", "ikke gemt");             }               if (combobox4.text != "" && listbox1.text != "")             {                 string ha = listbox1.selecteditem.tostring();                 clipboard.settext(combobox4.text + "stk " + ha + " farve " + combobox3.text);             }         }          private void logaftoolstripmenuitem_click(object sender, eventargs e)         {              this.close();             form fmlogind = new logind();             fmlogind.show();           }          private void easyaudtoolstripmenuitem_click(object sender, eventargs e)         {          }          private void luktoolstripmenuitem_click(object sender, eventargs e)         {             application.exit();         }          private void menustrip1_itemclicked(object sender, toolstripitemclickedeventargs e)         {          }              } } 

this line of code executes once , only once no matter how many times open form:

public static         sqlconnection con = new sqlconnection(@"data source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\sqlexpress;database=ha;persist security info=false; uid='" + globalvariables.user + "' ; pwd='" + globalvariables.psw + "'"); 

aside use of global variables (bad idea) , use of single static database connection (bad idea), once you've created connection first time always exist static property on type.

from point forward, can enter username , password values like. may updating global variables associated them, you're not updating con variable. right-hand side of expression doesn't hold reference global variables evaluated multiple times, evaluates them once , keeps result of evaluation.

so order of events in case is:

  1. create connection using supplied username , password.
  2. open connection.
  3. use form, connection, etc.
  4. close connection.
  5. change username , password variables, not connection.
  6. re-open original connection, using original connection string.

your best approach fixing remove sqlconnection object static scope. if want try initiate database connection when form loads, in constructor or in form1_load event handler. , instead of re-using same always-open connection throughout application, time need access database should create connection local scope , dispose of you're done it.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -