Reputation: 179
following is my code where I am declaring a column as private string and I am using the value to bind the value to grid but I am getting the error the input string was not in correct format near "PolicyRenewalGracePeriodDays "..please see the highlighted text below and advise me 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);**
Upvotes: 0
Views: 1102
Reputation: 48154
Most likely row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text
is not returning what you expect. If it has anything other than 0-9 or value larger than int.MaxValue
you'll get an exception. Additionally null
or System.String.Empty
will cause an exception.
You can use TryParse
instead which will return a bool indicating whether or not the parse worked. If it works, the int you pass in will be set to the string you pass in.
To give some actual code;
if(!int.TryParse(row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text, out renewalGracePeriodDays))
renewalGracePeriod = MyDefaultValue;
Upvotes: 4
Reputation: 216363
What happen if the cell doesn't contain a valid number (for example an empty string)?.
You get the exception mentioned in your question.
A simple workaround is to use the TryParse method
int renewalGracePeriodDays;
string temp = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text;
Int32.TryParse(temp, out renewalGracePeriodDays);
From the MSDN docs
When this method returns, [the second parameter] contains the 32-bit signed integer value equivalent to the number contained in s [the first parameter], if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized
Bold and text in square brakets have been added by me. So if your default value should be zero you don't have to do any test on the result of the TryParse method.
Upvotes: 2
Reputation: 1385
It's possible that the column you are checking is either an empty string, or contains non-integer data. I recommend using the TryParse method.
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
}
If the TryParse fails, your grace period will default to 0. It's a good idea to use this method when dealing with user inputs, because there's no telling what people will enter, even when properly prompted...
Upvotes: 1
Reputation: 63970
Besides the above answers, you can easily create a little extension method to handle these cases gracefully:
public static class ExtensionUtils
{
public static int ToZeroIfNotInt(this string valueToConvert)
{
int number =0;
int.TryParse(valueToConvert,out number);
return number;
}
}
And then call it as so:
renewalGracePeriodDays = row.Cells.FromKey(COL_RENEWAL_GRACE_PERIOD_DAYS).Text.ToZeroIfNotInt();
Upvotes: 2