2017-07-23 57 views
0

保護無效gv_card_RowUpdating(對象發件人,GridViewUpdateEventArgs E) {RowUpdating不工作顯示我輸入的字符串格式不正確

int result = 0; 
    CreditCard prod = new CreditCard(); 
    GridViewRow row = gv_card.Rows[e.RowIndex]; 
    string id = gv_card.DataKeys[e.RowIndex].Value.ToString(); 
    string tid = ((TextBox)row.Cells[0].Controls[0]).Text; 
    string tnumber = ((TextBox)row.Cells[1].Controls[0]).Text; 
    string texpirydate = ((TextBox)row.Cells[2].Controls[0]).Text; 
    string tcvv = ((TextBox)row.Cells[3].Controls[0]).Text; 
    string tcardtype = ((TextBox)row.Cells[4].Controls[0]).Text; 
    string tholdername = ((TextBox)row.Cells[5].Controls[0]).Text; 

    result = prod.CardUpdate(int.Parse(tid), tholdername, tnumber,texpirydate, int.Parse(tcvv), tcardtype); 
    if (result > 0) 
    { 
     Response.Write("<script>alert('Product updated successfully');</script>"); 
    } 
    else 
    { 
     Response.Write("<script>alert('Product NOT updated');</script>"); 
    } 
    gv_card.EditIndex = -1; 
    bind(); 
} 

}

以上是我的代碼,但它只是不能似乎更新我的網格視圖

+0

我要去猜測tid'的'值或'tcvv'失敗由於爲空或非數字值而解析爲「int」。如果您逐步瀏覽並閱讀「Parse」的文檔,這應該非常簡單。 – Crowcoder

回答

0

該消息很可能來自您致電int.Parse(string)的電話,該電話預期該字符串爲有效整數。要處理這個問題,你可以使用int.TryParse(string, out int),如果它能夠解析字符串,它將返回true或false。如果成功,out參數將包含解析的整數值。

所以你會首先嚐試解析整數字段。如果失敗,你可以返回一個錯誤信息,如果成功,那麼您可以在您的電話直接使用整數CardUpdate

int tidValue; 
int tcvvValue; 

if (!int.TryParse(tid, out tidValue)) 
{ 
    Response.Write("<script>alert('The value specified for TID is not an integer.');</script>"); 
} 
else if (!int.TryParse(tcvv, out tcvvValue)) 
{ 
    Response.Write("<script>alert('The value specified for TCVV is not an integer.');</script>"); 
} 
else 
{ 
    result = prod.CardUpdate(tidValue, tholdername, tnumber, texpirydate, tcvvValue, tcardtype); 

    if (result > 0) 
    { 
     Response.Write("<script>alert('Product updated successfully');</script>"); 
    } 
    else 
    { 
     Response.Write("<script>alert('Product NOT updated');</script>"); 
    } 
}