当我按下登录按钮时,有一个空白文本框会导致程序崩溃

时间:2016-08-10 13:43:55

标签: c# entity-framework

初学者程序员。我有一个登录按钮,当我输入正确的凭据时它完全正常,但是当我点击登录按钮时有一个空的文本框,程序崩溃并给我"未处理的类型'系统的例外。出现FormatException'发生在mscorlib.dll"。我尝试在其他地方使用!= null,但是那也没有用。所以我的问题是如何才能显示一个空的文本框"请输入有效的用户名和/或密码"而不是崩溃程序?谢谢!

其他信息:输入字符串的格式不正确。

private void btnLogin_Click(object sender, EventArgs e)
{
    Entities2 db = new Entities2();
    foreach (var usert in db.Teachers)
    {
        if (usert.TID == Convert.ToInt32(txtLogin.Text) && usert.Password == txtPassword.Text)
        {
            Teach teacher = new Teach();
            teacher.ShowDialog();
        }
        else if (usert.TID != Convert.ToInt32(txtLogin.Text) && usert.Password != txtPassword.Text)
        {
            MessageBox.Show("Please Enter a Valid Username and/or Password");
        }
    }
}

5 个答案:

答案 0 :(得分:2)

在任何事情之前检查Textbox是否为“” 见下文:

private void btnLogin_Click(object sender, EventArgs e)
{

    if (txtLogin.Text != "")
    {

        Entities2 db = new Entities2();
        foreach (var usert in db.Teachers)
        {
             if (usert.TID == Convert.ToInt32(txtLogin.Text) && usert.Password == txtPassword.Text)
             {
                  Teach teacher = new Teach();
                  teacher.ShowDialog();
             }
             else if (usert.TID != Convert.ToInt32(txtLogin.Text) && usert.Password != txtPassword.Text)
             {
                  MessageBox.Show("Please Enter a Valid Username and/or Password");
             }
        }           
     }
}

答案 1 :(得分:2)

private void btnLogin_Click(object sender, EventArgs e)
{
   try
   {
       if(txtLogin.Text!="" && txtPassword.Text!="")
       {
          Entities2 db = new Entities2();
          foreach (var usert in db.Teachers)
          {
             if (usert.TID == Convert.ToInt32(txtLogin.Text) &&  usert.Password == txtPassword.Text)
            {
                Teach teacher = new Teach();
                teacher.ShowDialog();
            }
            else if (usert.TID != Convert.ToInt32(txtLogin.Text) && usert.Password != txtPassword.Text)
            {
                MessageBox.Show("Please Enter a Valid Username and/or Password");
            }
        }
      else
      {
         if(txtLogin.Text=="")
         {
            MessageBox.Show("Please Enter a Username");
         }
         else if(txtPassword.Text=="") 
         {
            MessageBox.Show("Please Enter a  Password");
         }
      }
    }
    Catch(Exception ex)
    {
       MessageBox.Show("Please Enter a  Valid Username and/or Password");
    }
}

此外,这不是检查和匹配UserName密码的正确方法。您可以改为使用以下方法

if(txtLogin.Text!="")
   {
      Entities2 db = new Entities2();
      Teacher Tobj=db.Teachers.where(x=>x.TID==Convert.ToInt32(txtLogin.Text) && x.Password==txtPassword.Text).SingleOrDefault();
        if (Tobj!=null)
        {
            Teach teacher = new Teach();
            teacher.ShowDialog();
        }
        else
        {
            MessageBox.Show("Please Enter a Valid Username and/or Password");
        }
   }

答案 2 :(得分:0)

您可以尝试解析。如果它无法解析,它将返回false并且您可以进行一些错误处理(感谢Phil)。

int userId = 0;
if(int.TryParse(txtLogin.Text, out userId)){
   // err handling
}

答案 3 :(得分:0)

private void btnLogin_Click(object sender, EventArgs e)
{
    try 
    {
        <body of your btnLogin_Click()>
    } 
    catch(FormatException ex)
    {
        MessageBox.Show("Please Enter a Valid Username and/or Password");
    }
    catch(Exception ex2)
    {
        MessageBox.Show("Error: " + ex2.Message);
    }
}

答案 4 :(得分:0)

首先尝试解析输入的用户ID,如下所示:

private void btnLogin_Click(object sender, EventArgs e)
{
    int userId;

    if (int.TryParse(txtLogin.Text, out userId))
    {
       Entities2 db = new Entities2();
       foreach (var usert in db.Teachers)
       {
           if (usert.TID == userId && usert.Password == txtPassword.Text)
           {
              Teach teacher = new Teach();
              teacher.ShowDialog();
           }
           else
           {
              MessageBox.Show("Please Enter a Valid Username and/or Password");
           }
       }
    }
    else
    {
         MessageBox.Show("Please Enter a Valid User ID");

    }
}