Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get an error when I run it, it says Input string was not in a correct format. can someone please assist, I have tried everything

C#
public partial class Test : System.Web.UI.Page
    {
        private void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                {
                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                    MonthNumber = a
                }
                );
                ddlMonth.DataBind();
                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
                ddlYear.DataBind();
                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                ddlday.DataBind();
            }
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        { ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue))); ddlday.DataBind(); }

        public bool IsPostBack { get; set; }
    }
}
Posted
Updated 17-Aug-12 0:38am
v2
Comments
Malli_S 17-Aug-12 6:39am    
Please do post what compile error you get.

I think in Convert.ToInt32(ddlMonth.SelectedValue); ...
'ddlMonth.SelectedValue' is not a valid integer string.

//Check it first//
MessageBox.show(ddlMonth.SelectedValue.toString());
it will show integer string.
Make sure this is a valid integer string.
 
Share this answer
 
Try this

C#
private void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                {
                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                    MonthNumber = a
            //DataTextField = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
            //DataValueField = a
              }
                );

        //If this code is not working then try the above code in the loop.
        ddlMonth.DataTextField="MonthName";
        ddlMonth.DataValueField="MonthNumber";

                ddlMonth.DataBind();
                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
                ddlYear.DataBind();
                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                ddlday.DataBind();
            }
        }
 
Share this answer
 
Comments
MarkNinja 17-Aug-12 7:19am    
Why is it not validating the how many days there are in a month?
Replace

ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));

with

ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedIndex + 1)));

I think this will solve your issue.
 
Share this answer
 
Comments
MarkNinja 17-Aug-12 7:44am    
I see it is not evening stepping into the code
MarkNinja 17-Aug-12 8:28am    
I came right thanks i enabled postback on the dropdown list and set the ddlmonth event to point to the that event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900