For this kind of cases, every time we create different and lot of if conditions. Finally, i have created a ConvertToNullable functions for myself. Here is the simple ConvertToNullable function to convert object to nullable integer.
public static Nullable<Int32> ConvertToNullableInt32(string value) { Nullable<Int32> convertedValue = null; if (!string.IsNullOrEmpty(value)) { Int32 converted = 0; if (Int32.TryParse(value.ToString(), out converted)) { convertedValue = converted; } } return convertedValue; }
you can extend or create similar functions for your needs.
0 comments:
Post a Comment