2012-11-16 127 views
0

以下是我的代碼,我聲明一個列爲私有字符串,我使用該值綁定到網格的值,但我得到的錯誤輸入字符串不正確的格式近「PolicyRenewalGracePeriodDays」 ..please看到高亮文本的下方,建議我請輸入字符串格式不正確

/// </summary> 
private const string COL_UNDERWRITER_DISPLAY_NAME = "UnderwriterDisplayName"; 
/// <summary> 
/// 
/// </summary> 
private const string COL_UNDERWRITER_INITIALS = "UnderwriterInitials"; 
/// <summary> 
/// 
/// </summary> 
private const string COL_UA_DISPLAY_NAME = "UADisplayName"; 
/// <summary> 
/// 
/// </summary> 
private const string COL_UA_INITIALS = "UA"; 
**private const string COL_RENEWAL_GRACE_PERIOD_DAYS = "PolicyRenewalGracePeriodDays";** 
#endregion 


    protected void grdAction_DataBound(object sender, EventArgs e) 
     { 
      foreach (UltraGridRow row in this.grdAction.DisplayLayout.Rows) 
      { 
       TemplatedColumn col; 
       CellItem item; 
       HyperLink docLink; 
       HyperLink letterLink; 
       HyperLink actionLink; 
       Label actionLabel; 
       var policyClassId = Utility.GetCurrentPolicyClassId(); 
      PolicyClass policyClass = Utility.GetCurrentPolicyClassEntity(); 
       var accountId = (int) row.DataKey; 
       var insuredName = row.Cells.FromKey(COL_INSURED_NAME_HIDDEN).Text; 
       var referenceNumber = row.Cells.FromKey(COL_REFERENCE_NUMBER).Text; 
       var statusId = int.Parse(row.Cells.FromKey(COL_STATUS_ID).Text); 

       var optionNames = string.Empty; 
       if (!string.IsNullOrEmpty(row.Cells.FromKey(COL_OPTION_NAMES).Text)) 
        optionNames = row.Cells.FromKey(COL_OPTION_NAMES).Text; 

       var optionCount = int.Parse(row.Cells.FromKey(COL_OPTION_COUNT).Text); 
       var isVoidable = (row.Cells.FromKey(COL_IS_VOIDABLE).Text == "1"); 
       bool renewalFlag; 
       bool doNotRenewFlag; 
       bool hasRenewingReferenceNumber; 
       var currentUser = (User) Session[AppConstants.SK_CURRENT_USER]; 
       var expirationDate = DateTime.MinValue; 
       bool convertedFlag; 
       var documentCount = int.Parse(row.Cells.FromKey(COL_DOCUMENT_COUNT).Text); 
       var allowAddLayer = bool.Parse(row.Cells.FromKey(COL_ALLOW_ADD_LAYER).Text); 
       var renewableLayers = row.Cells.FromKey(COL_RENEWABLE_LAYERS).Text; 
       int renewalGracePeriodDays = 0; 

       **renewalGracePeriodDays = int.Parse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text);** 
+2

我的'row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text'包含字符,然後'0-9':) –

+1

您需要識別'row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS)的值。文本'在發生異常時。然後更新您的問題以包含該值。 –

+1

'int.Parse(「thisIsNotAValidNumber」)'會拋出一個異常,就像你看到的那樣。檢查文本字符串是什麼。它可能是空的或包含字母或其他東西。 –

回答

2

如果電池不包含有效數字(例如一個空字符串),會發生什麼?
你會在你的問題中提到異常。
簡單的解決方法是使用TryParse方法

int renewalGracePeriodDays; 
string temp = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text; 
Int32.TryParse(temp, out renewalGracePeriodDays); 

從MSDN文檔

此方法返回時,[第二個參數]包含32位帶符號的整數值 等效到第[第一個參數]中包含的數字,如果轉換成功,則 或如果轉換失敗則爲零。如果s 參數爲空,格式不正確,或者表示編號小於MinValue或大於MaxValue的 ,轉換將失敗。這個參數是 通過未初始化

粗體和正方形的文本已添加我。所以如果你的默認值應該是零,你不必對TryParse方法的結果做任何測試。

4

最有可能的row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text沒有返回你所期望的。如果它不是0-9或值大於int.MaxValue,則會發生異常。此外,nullSystem.String.Empty將導致異常。

您可以使用TryParse代替它,它將返回一個布爾值,指示解析是否工作。如果它工作,你通過的int將被設置爲你傳遞的字符串。

給一些實際的代碼;

if(!int.TryParse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text, out renewalGracePeriodDays)) 
     renewalGracePeriod = MyDefaultValue; 
+0

感謝您的回覆,我正嘗試使用您提供給我的代碼,並說它有一些無效的參數。任何想法? – user1567194

+0

@ user1567194我的一個變量名稱是錯誤的。我只是修復它。想不到任何其他......這些是在msdn上指定的參數http://msdn.microsoft.com/en-us/library/f02979c7.aspx – evanmcdonnal

2

除了上述的回答,您可以輕鬆地創建一個小擴展方法來妥善地處理這些案件:

​​

然後調用它像這樣:

renewalGracePeriodDays = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text.ToZeroIfNotInt(); 
+0

如果某些值真的是'0',那麼有些字符無效? –

+0

@ L.B。它仍會正確返回值。如果該值無法解析,它將簡單地將其默認爲「0」。您可能可以向擴展方法添加一個參數,以選擇除0以外的默認值。例如,您可能想要返回-1,以指示無效值... – Icarus

+0

Icarus,假設該字段包含整數爲十六進制字符串你會爲1a返回0,0也爲0。真正的問題是OP爲什麼得到這個消息,而不是它如何被抑制。 –

1

這是可能的您正在檢查的列是一個空字符串,或者包含非整數數據。我建議使用TryParse方法。

int renewalGracePeriodDays; 
if (!int.TryParse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text), out renewalGracePeriodDays) 
{ 
    renewalGracePeriodDays = 0; 
    // Inside here, you can log the exception, alert the user, or end processing 
} 

如果失敗的TryParse,你的寬限期將默認爲0。這是一個與用戶輸入打交道時使用這個方法是一個好主意,因爲有沒有說什麼人會進入,即使正確提示...

+0

埃文擊敗了我。大鼠。 – MadHenchbot