Another simple use of extension methods!!
The small issue he faces that he need to parse the value selected in one line of code and make sure that if the user does not select a value the default value will be parsed to null and do not cause a problem -in each drop-down there is a "[Select]" item with empty string value -
I wondered a int.Parse function returns null when the parsed value is null but throws "Input string was not in a correct format" exception when try to parse an empty string. And when use int.TryParse it will result with 0 value when parsing operation failed.
In such simple scenario extension methods proof it self. Simply we can create an extension method call it SafeParse for example ....
{
if (string.IsNullOrEmpty(Value))
return null;
else
return int.Parse(Value);
}
MyNullableValue = DropDownList1.SelectedValue.SafeParse();