Reputation: 99
I have some textboxes where the user enters a date or a time. When I save on database I create
string input = txtdocumentiDate.Text +" "+ txtdocumentiTime.Text;
ts.Documenti = DateTime.ParseExact(input, "dd/MM/yyyy HH.mm", CultureInfo.InvariantCulture);
(ts is my entity framework database table)
I would want to make a check to see if all the formats of my textboxes are valid before fire button save.
Upvotes: 4
Views: 3146
Reputation: 1264
Use DateTime.TryParse
. It returns a bool value indicating whether the method was able to parse the string or not.
string input = txtdocumentiDate.Text +" "+ txtdocumentiTime.Text;
DateTime dummy;
if(DateTime.TryParse(input, dummy))
ts.Documenti = DateTime.ParseExact(input, "dd/MM/yyyy HH.mm", CultureInfo.InvariantCulture);
Upvotes: 2