I have an application where I need to get the years from the Date of Birth, I am using dropdownlist to populate Day, Month and Year.
I have coded on year dropdownlist selectindexchange event to calculate years here my code, it runs perfectly when I do it on my development machine but as I host it on godaddy it gives me error :
String was not recognized as a valid DateTime.
I have tried few steps as in to put cultureinfo which I have comment but even those do not work
protected void DDLyear_SelectedIndexChanged(object sender, EventArgs e)
{
var date = DDLDay.Text + "/" + DDLMonth.Text + "/" + DDLYear.Text;
DOB.Text = date;
if (DDLDay.Text == "DD" || DDLMonth.Text == "MM" || DDLYear.Text == "YYYY")
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Date')</script>");
DDLYear.Text = "YYYY";
return;
}
if (Convert.ToDateTime(date) > System.DateTime.Now)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('DOB cannot be greater then current date')</script>");
//DOB.Text = System.DateTime.Now.ToString("dd/MM/yyyy");
return;
}
// DateTime now = System.DateTime.Now;
//DateTime now = DateTime.Parse(System.DateTime.Now.ToString("yyyyMMdd"), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
//DateTime birthDate = Convert.ToDateTime(DOB.Text, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
DateTime now = DateTime.ParseExact(System.DateTime.Now.ToString(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
string birthDate = Convert.ToDateTime(DOB.Text).ToString("dd/MM/yyyy");
TimeSpan difference = Convert.ToDateTime(now) - Convert.ToDateTime(birthDate);
//DateTime birthDate = Convert.ToDateTime(DOB.Text);
//TimeSpan difference = Convert.ToDateTime(now) - Convert.ToDateTime(birthDate);
int days = (int)difference.TotalDays;
int year = days / 365;
////int remain = days % 365;
////int month = remain / 30;
////int day = remain % 30;
txtage.Text = year + " " + "Years".ToString();
}
Stack Trace
[FormatException: String was not recognized as a valid DateTime.]
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +10973474
System.Convert.ToDateTime(String value) +83 FAHIS_Data_Entry_Software_in_WEB.NewEnrolee5.DDLyear_SelectedIndexChanged(Object sender, EventArgs e) +397
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +116
System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +133
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +13
System.Web.UI.Page.RaiseChangedEvents() +132
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1644