date time is not in format [Resolved]

Posted by Sashi under ASP.NET on 3/10/2016 | Points: 10 | Views : 304 | Status : [Member] | Replies : 1
this is my code ... i am getting error saying "
date string is not in format" how to over come from this problem

string arrivalcity = (gvr1.FindControl("Arrival_city") as TextBox).Text;
string arrivalhours = (gvr1.FindControl("ddlahrs") as DropDownList).SelectedValue;
string arrivalmins = (gvr1.FindControl("ddlamin") as DropDownList).SelectedValue;

DateTime arrivaldate = Convert.ToDateTime((gvr1.FindControl("Arrival_date") as TextBox).Text);


Posted by: A2H on: 3/10/2016 [Member] [MVP] Silver | Points: 50


Most likely the reason for exception could be the DateTime is not in proper format. You might consider using DateTime.Parse method to check if input value is of proper format and then convert the value like below

   DateTime arrivaldate;
//Check if input date value is of proper format
if (DateTime.TryParse((gvr1.FindControl("Arrival_date") as TextBox).Text, out arrivaldate))
//Your can use arrivaldate here

This will return DateTime.Min value instead of throwing error if input value is not in proper date format
You can check this link for more details:

My Blog

Sashi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response