2016-09-26 1 views
-3

テキストボックス(tAge)に入力したときにグリッドビューの人のレコードを更新しようとしていますが、それは私に例外が表示されます入力文字列は正しい形式。私は多くのコードを試していましたが、私を助けてくれて助けてくれません。そしては、に前もって感謝しています。 は、ここでは人の詳細テキストボックスの値をintに変換し、それが空かどうかを確認する

私は適切な値に割り当てる前に最初の Textプロパティをチェックすることをお勧め
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) 
{ 
    int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); 
    int intResult = 0; 
    GridViewRow row = GridView1.Rows[e.RowIndex]; 
    TextBox tFN = (TextBox)row.FindControl("txtFName"); 
    TextBox tLN = (TextBox)row.FindControl("txtLName"); 
    TextBox tAge = (TextBox)row.FindControl("txtAge"); 
    // instantiate BAL 
    PersonBAL pBAL = new PersonBAL(); 
    PersonBO person = new PersonBO(); 
    try 
    { 
     person.PersonID = personID; 
     person.FirstName = tFN.Text; 
     person.LastName = tLN.Text; 
     person.Age = Int32.Parse(tAge.Text); 
     //if (String.IsNullOrEmpty(String.Trim(tFN.Text))) 
     if (tFN.Text.Trim() == "") 
     { 
      lblMessage.Text = "Name can't be Blank"; 
     } 
     else if (tLN.Text.Trim() == "") 
     { 
      lblMessage.Text = "LastName can't be Blank"; 
     } 
     else if (tAge.Text.Trim()=="") 
     { 
      lblMessage.Text = "Age can't be Blank"; 
     } 
     else 
     { 
      intResult = pBAL.Update(person); 
     if (intResult > 0 && tFN.Text != "") 
     { 
      string message = "Updated Successfully!"; 
      string script = "window.onload = function(){ alert('"; 
      script += message; 
      script += "');"; 
      script += "window.location = '"; 
      script += Request.Url.AbsoluteUri; 
      script += "'; }"; 
      ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true); 
     } 
     //else if(tFN.Text == "") 
     //{ 
     //} 
     else 
     { 
      string message = "Already Exist!"; 
      string script = "window.onload = function(){ alert('"; 
      script += message; 
      script += "');"; 
      script += "window.location = '"; 
      script += Request.Url.AbsoluteUri; 
      script += "'; }"; 
      ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true); 
      //lblMessage.Text = message.ToString(); 
     } 
    } 
    } 
    catch (Exception ee) 
    { 
     lblMessage.Text = ee.Message.ToString(); 
    } 

    finally 
    { 
     person = null; 
     pBAL = null; 
    } 
    GridView1.EditIndex = -1; 
    // Refresh the list 
    BindGrid(); 
} 
+0

これはMVCではなくWebフォームコードです。 –

+1

'if(String.IsNullOrEmpty(tFN.Text))が返されるようにするか、大文字小文字を処理したい場合は' if(String.IsNullOrEmpty(tFN.Text)){..空またはnullがあれば何か値...}; ' –

答えて

0

を更新するための私のコードです。変換のためには、を返すと、boolが返され、形式が正しい場合のみ解析されます。

try 
{ 
    // ask whether it is blank or full of spaces 
    if (string.IsNullOrWhiteSpace(tFN.Text)) 
    { 
     lblMessage.Text = "Name can't be Blank"; 
    } 
    else 
    { 
     person.FirstName = tFN.Text; 
    } 

    // do the same again for the last name 
    if (string.IsNullOrWhiteSpace(tLN.Text)) 
    { 
     lblMessage.Text = "Name can't be Blank"; 
    } 
    else 
    { 
     person.FirstName = tLN.Text; 
    } 


    // use TryParse for the age 
    int p_age; 
    if (Int.TryParse(tAge.Text, out p_age)) 
    { 
     person.Age = p_age; 
    } 
    else 
    { 
     lblMessage.Text = "Age is in incorrect Format"; 
    } 

    .... 
関連する問題