I am trying to validate two data of birth text box fields if the member does not enter in these two boxes an error message will popped out like "pls enter valid date format"
by using the following code
if (tbStartDate.Text != "0" && tbEndDate.Text != "0")
{
string dob = tbStartDate.Text;
DateTime dv = DateTime.Parse(dob);
string format1 = dv.ToString("yyyy-MM-dd");
string dob2 = tbEndDate.Text;
DateTime dt2 = DateTime.Parse(dob2);
string format2 = dt2.ToString("yyyy-MM-dd");
}
else
{
MessageBox.Show("pls enter valid date ");
}
when i am trying to save with out entering the textbox values it was showing error like this "String was not recognized as a valid DateTime".
can any one help on this....
this is modified code still it was showing error at Datetime DV;
private void btnCTimetablessave_Click(object sender, EventArgs e)
{
string dob = tbStartDate.Text;
DateTime dv;
if (!DateTime.TryParse(dob, out dv))
{
MessageBox.Show("pls enter a valid start date");
return;
}
string format1 = dv.ToString("yyyy-MM-dd");
string dob2 = tbEndDate.Text;
DateTime dt2;
if (!DateTime.TryParse(dob2, out dt2))
{
MessageBox.Show("pls enter valid end date");
return;
}
string format2 = dt2.ToString("yyyy-MM-dd");
still it was showing error like this "String was not recognized as a valid DateTime" at datetime Dv;
and this is modified code third time
private void btnCTimetablessave_Click(object sender, EventArgs e)
{
string dob = tbStartDate.Text;
DateTime dv;
if (!DateTime.TryParse(dob, out dv))
{
MessageBox.Show("pls enter a valid start date");
return ;
}
string format1 = dv.ToString("yyyy-MM-dd");
string dob2 = tbEndDate.Text;
DateTime dt2;
if (!DateTime.TryParse(dob2, out dt2))
{
MessageBox.Show("pls enter valid end date");
return;
}
string format2 = dt2.ToString("yyyy-MM-dd");
}