C# 表单关闭返回一个错误
我有一个c#应用程序,它有一个使用SQLServer自定义用户表进行身份验证的登录名 我面临的问题是:当我退出应用程序时,它会抛出以下错误消息 可能的解决办法是什么 通用代码为:C# 表单关闭返回一个错误,c#,C#,我有一个c#应用程序,它有一个使用SQLServer自定义用户表进行身份验证的登录名 我面临的问题是:当我退出应用程序时,它会抛出以下错误消息 可能的解决办法是什么 通用代码为: private void logIn() { try { if (txtpwd.Text == "" || txtusername.Text == "") { MessageBox.Sh
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;
}
}
}
你有问题。必须使用方法与UI元素(表单)交互:
this.Invoke(新函数(()=>fsm.ShowDialog(this));
提供了真正可行的解决方案。我可以用它来解决各种类型的多线程问题吗?
this.Invoke(new Func<DialogResult>(() => fsm.ShowDialog(this)));