2012-12-03 44 views
0

我有一个登录表单并修改了一些来自其他来源的代码,以使其适用于我的情况。当登录无效时,我收到错误,因为该号码为空或“无限”。登录无效指定演员表无效

public int Validate_Login(String Username, String Password) 
{ 

    //SqlConnection con = new SqlConnection(@"User id=judgment_day;Password=Kit$hen;Server=216.40.232.82, 2153;Database=new_judgment-system"); 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CityCollectionCSharp.Properties.Settings.newCityCollectionConnectionString"].ConnectionString); 
    SqlCommand cmdselect = new SqlCommand(); 
    cmdselect.CommandType = CommandType.StoredProcedure; 
    cmdselect.CommandText = "[dbo].[prcLoginv]"; 
    cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = Username; 
    cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = Password; 
    cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4); 
    cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output; 
    cmdselect.Connection = con; 
    int Results = 0; 

    try 
    { 
     con.Open(); 
     cmdselect.ExecuteNonQuery(); 
     Results = (int)cmdselect.Parameters["@OutRes"].Value; 
    } 
    catch (SqlException ex) 
    { 
     lblMessage.Text = ex.Message; 
    } 
    finally 
    { 
     cmdselect.Dispose(); 

     if (con != null) 
     { 
      con.Close(); 
     } 
    } 
    return Results; 
} 

错误发生在这条线:

Results = (int)cmdselect.Parameters["@OutRes"].Value; 

我怎么会去的错误处理,如此,当它是不是一个有效的整数,这将触发无效登录该代码是在这里:

protected void btnlogin_Click(object sender, EventArgs e) 
{ 
    int Results = 0; 


    if (txtUsername.Text != null && txtPassword.Text != null) 
    { 
     Results = Validate_Login(txtUsername.Text, txtPassword.Text); 

     if (Results > 0) 
     { 
      lblMessage.Text = "Login is Good"; 

      GlobalVariables.Globals.SetGlobalInt(Results); 
      lblMessage.Text = "'" + GlobalVariables.Globals.GlobalInt + "'"; 
      this.Hide(); 
      frmSwitch frm = new frmSwitch(); 
      frm.Show(); 
     } 
     else 
     { 
      lblMessage.Text = "Invalid Login"; 
      lblMessage.ForeColor = System.Drawing.Color.Red; 
     } 
    } 
    else 
    { 
     lblMessage.Text = "Please make sure that the username and the password is Correct"; 
    } 
} 

回答

1

你似乎有大部分的代码。我认为你并不了解如何处理例外情况 - 请阅读Exception Handling,你应该能够遵循下面的例子。

由于您没有指定确切的Exception被提出,我添加了一个通用的Excepion处理程序。

try 
    { 
     con.Open(); 
     cmdselect.ExecuteNonQuery(); 
     // Check for null values 
     if (cmdselect.Parameters["@OutRes"].Value != DBNull.Value) 
     { 
      Results = (int)cmdselect.Parameters["@OutRes"].Value; 
     } 
    } 
    catch (SqlException ex) 
    { 
     lblMessage.Text = ex.Message; 
    } 
    catch (Exception generalEx) 
    { 
     // Do something with the error - Just displaying for now 
     lblMessage.Text = ex.Message; 
    } 
    finally 
    { 
     cmdselect.Dispose(); 

     if (con != null) 
     { 
      con.Close(); 
     } 
    } 

该代码仅用于说明,根据需要进行编辑。

+0

谢谢Kami。我认为我很懒,忘记了if语句。这最初设置为0或1,但我需要将实际情况传递给我的软件的其他部分,并错过了主要部分。谢谢! – korrowan

0

试试这个:

try 
{ 
    con.Open(); 
    cmdselect.ExecuteNonQuery(); 
    object o = (int)cmdselect.Parameters["@OutRes"].Value; 
    if (o == DBNull.Value) 
     return 0; 
    return (int)o; 
} 
catch (SqlException ex) 
{ 
    lblMessage.Text = ex.Message; 
} 
finally 
.... 

finally块占用资源的照顾。