Do not convert it to string using 'ToShortDateTimeString', just set the result to Convert.ToDateTime :
myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text);
Assuming you have done a sufficient check on txtScoringUpgradeDate.Text ?
My preference when working with these type conversions is to use the TryParse method, for example:
DateTime date; if (DateTime.TryParse(txtScoringUpgradeDate.Text, out date)) myObject.ScoringLastUpgrade = date;
Convert.ToDateTime , just like an explicit DateTime.Parse will InvalidCastException when an InvalidCastException value occurs. It is better to make a tollerant error code, then the needles catch an exception.
UPDATE : based on the last comment:
You should not return DateTime.MinValue in this case, since MinValue less than the supported minimum value of the datetime column. CLR datetime supports a date range up to 0000-01-01, while SQL datetime (as well as the comparative CLR type SqlDateTime ) supports a minimum value of 1753-01-01. Since this is a value with a datetime value of zero, you must set it to null:
public static DateTime? ToNullableDateTime(this string date) { DateTime dateTime; return (DateTime.TryParse(date, out dateTime)) ? (DateTime?)dateTime : null; }
source share