簡體   English   中英

輸入字符串格式不正確

[英]The Input string was not in correct format

以下是我的代碼,其中我將一列聲明為私有字符串,並使用該值將值綁定到網格,但出現錯誤,輸入字符串在“ PolicyRenewalGracePeriodDays”附近的格式不正確。請查看突出顯示的文本請在下面告訴我

/// </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);**

最可能是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;

如果單元格中不包含有效數字(例如空字符串)會發生什么?
您得到問題中提到的例外。
一個簡單的解決方法是使用TryParse方法

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

來自MSDN文檔

返回此方法后,如果轉換成功,則[第二個參數]包含與s [第一個參數]中包含的數字等效的32位有符號整數值;如果轉換失敗 ,則為零 如果s參數為null,格式不正確或表示小於MinValue或大於MaxValue的數字,則轉換將失敗。 此參數未初始化傳遞

我添加了粗體和方格文字。 因此,如果您的默認值應為零,則不必對TryParse方法的結果進行任何測試。

除了上述答案外,您還可以輕松地創建一個擴展方法來優雅地處理這些情況:

public static class ExtensionUtils
{
    public static int ToZeroIfNotInt(this string valueToConvert)
    {
        int number =0;
        int.TryParse(valueToConvert,out number);
        return number;
    }
}

然后這樣稱呼:

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

您正在檢查的列可能為空字符串或包含非整數數據。 我建議使用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。在處理用戶輸入時,最好使用此方法,因為即使正確提示,也不會告訴人們將輸入什么內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM