WPF C # How to validate textbox input as date format
I have text boxes where the user enters a date or time. When I save to the 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 database database)
I would like to make a check to check if all my textbox formats are valid before saving the save button.
+3
source to share
1 answer
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);
+2
source to share