2015-08-15 114 views
1

我有一個c#應用程序有一個登錄,它使用sql server自定義用戶表進行身份驗證。窗體關閉返回一個錯誤

我面對的問題是:當我退出應用程序時,它會引發以下錯誤消息。

enter image description here

什麼將是一個可能的解決方案?

一般的代碼是:

private void logIn() 
     { 
      try 
      { 
       if (txtpwd.Text == "" || txtusername.Text == "") 
       { MessageBox.Show("Field is Blank!", "SQL Server", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } 


       int i = 0; // we use this variable to count if ther’s a user with this name 

       conn = new SqlConnection(Scon.ReturnConnection()); 
       SqlCommand cmd = new SqlCommand(); 
       cmd.CommandText = "select * from Users"; 
       cmd.Connection = conn; 
       conn.Open(); 
       //cmd.Parameters.AddWithValue("@type", cmbType.Text); 
       SqlDataReader sdr = cmd.ExecuteReader(); 


       while (sdr.Read()) 
       { 
        string un = sdr["userid"].ToString(); 
        string pd = sdr["pwd"].ToString(); 
        string ut = sdr["usertype"].ToString(); 

        if (un == txtusername.Text) 
        { 
         ++i; 
         if (pd == txtpwd.Text) 
         { 
          Form panel; 
          this.Opacity = 0; 
          switch (ut) 
          { 
           case "admin": 

            panel = new GeneralSetting(); 
            LoginRegister(); 
            panel.Show();         

            break; 
           case "user": 
            panel = new frmStockManagement(); 

             if (fsm == null) 
             { 
              this.Hide();           
              fsm = new frmStockManagement(); //Create form if not created 
              fsm.FormClosed += (s, args) => this.Close(); //Add eventhandler to cleanup after form closes 
              Thread.Sleep(3000); 
             } 
             LoginRegister(); 

             fsm.ShowDialog(this); //Show Form assigning this form as the forms owner 

            break; 
          } 
         } 
         else 
         { 
          MessageBox.Show("Wrong Password!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         } 
       } 
       } 
       if (i == 0) 

      MessageBox.Show("No specified user with this name!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); 

      } 
      catch (SqlException sEx) 
      { 
       MessageBox.Show(sEx.Message, "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      finally 
      { 
       conn.Close(); 
      } 
     } 

     void fsm_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      // fsh = null; //If form is closed make sure reference is set to null 
      Show(); 
     } 
     private void button2_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 
     private bool IsServerConnected() 
     { 

      using (var l_oConnection = new SqlConnection(Scon.ReturnConnection())) 
      { 
       try 
       { 
        l_oConnection.Open(); 
        return true; 
        //MessageBox.Show("Connection Availabel!", "SQL Server", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
       catch (SqlException) 

       { 
        MessageBox.Show("No Connection to the server", "SQL Server", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        GeneralSetting setting = new GeneralSetting(); 
        setting.ShowDialog(); 
        this.Hide(); 
        this.Close();     
        return false; 
       } 
      } 
     } 

回答

2

你有multy-threading問題。您必須使用Control.Invoke方法與UI元素(表單)進行交互:

this.Invoke(new Func<DialogResult>(() => fsm.ShowDialog(this))); 
+0

提供了真正可行的解決方案。我可以將它用於每種類型的多線程問題嗎? –

+0

@NurSelam for winforms多線程問題 – Backs

+0

好的,謝謝@Backs –