表单关闭会返回错误

时间:2015-08-15 11:50:06

标签: c#

我有一个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;
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您遇到multy-threading问题。您必须使用Control.Invoke方法与UI元素(表单)进行交互:

final Server s = Server.createWebServer();
s.start().openBrowser(s.getURL());