DateTime? billDateFrom = string.IsNullOrWhiteSpace(txtBillDateFrom.Text) ? null : (DateTime?)DateTime.Parse(txtBillDateFrom.Text);
txtBillDateFrom.Text equals “05/26/2011”. (26 may 2011)
This code throws an error “String was not recognized as a valid DateTime” only in a web application. In a console application it works good.
Why? Does it depend on locale?
Yes, the behavior of Parse() depends on the current thread’s culture. You can use ParseExact() to make that behavior consistent, regardless of that culture: