0


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
mark
  • 623
  • 3
  • 21
  • 54
  • 1
    On which line exactly you get this error? What is your `CurrentCulture`? – Soner Gönül May 30 '15 at 11:23
  • CurrentCultire is `hi-In`,I will post my stack trace – mark May 30 '15 at 11:24
  • I am not sure which line it should be according to me `string birthDate = Convert.ToDateTime(DOB.Text).ToString("dd/MM/yyyy");` – mark May 30 '15 at 11:29
  • What is the value of `DOB.Text` exactly? Debug your code and tell us. – Soner Gönül May 30 '15 at 11:30
  • `DOB.text="01/01/2002"` – mark May 30 '15 at 11:31
  • `DOB.Text="01/01/2002"` I have even tried with `DOB.Text="15/01/2002"` – mark May 30 '15 at 11:33
  • Why don't you just parse the individual parts? E.g. `Int32.Parse(DDLDay.Text)` – NeddySpaghetti May 30 '15 at 11:36
  • @mark Are you sure your current culture is `hi-In`? Because both `Convert.ToDateTime("01/01/2002", CultureInfo.GetCultureInfo("hi-In"))` and `Convert.ToDateTime("15/01/2002", CultureInfo.GetCultureInfo("hi-In"))` parses fine. – Soner Gönül May 30 '15 at 11:37
  • @SonerGönül no I am not, I previously had textbox which showed calender, but due to client requirement had to change , and I use `hi-In` and it worked fine,so I guessing it should be right – mark May 30 '15 at 11:39
  • By the way - you're calculating age incorrectly. See [How do I calculate someone's age in C#?](http://stackoverflow.com/q/9/634824) – Matt Johnson-Pint Jun 01 '15 at 03:15

1 Answers1

0

User Culture Info

 cInfo = new CultureInfo("es-AR");
        DateTime DOB= Convert.ToDateTime(DOB.Text, cInfo.DateTimeFormat);
  • `cInfo = new CultureInfo("es-AR");` do I need to change it to `cInfo = new CultureInfo("hi-In");` ? – mark May 30 '15 at 11:32