2012-12-03 4 views
0

로그인 양식이 있고 다른 상황의 코드가 수정되어 상황에 맞게 작동합니다. 숫자가 비어 있거나 '무한'하므로 로그인이 유효하지 않은 경우 오류가 표시됩니다. 내가 처리 오류에 대한 갈 것이라고 어떻게로그인이 유효하지 않습니다. 지정한 캐스트가 유효하지 않습니다.

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

그것은 코드가 여기에 잘못된 로그인을 트리거하는 유효한 정수가 아닌 너무 때 :

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; 
} 

오류는이 라인에서 발생합니다 :

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(); 
     } 
    } 

코드는 단지 예이며 필요에 따라 편집 할 수 있습니다.

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 블록은 자원을 담당 :

+0

감사합니다. 카미. 나는 게으르다가 if 문을 잊었다 고 생각한다. 이것은 원래 0 또는 1로 설정되었지만 소프트웨어의 다른 부분에 실제 정보를 전달하고 중요한 부분을 놓칠 필요가있었습니다. 감사! – korrowan

0

이보십시오.

관련 문제