Fill Years to Dropdownlist dynamically with some range in Asp.net


Filling Dropdownlist dynamically with the range of 1900 to this Year, for that
write the following code in PageLoad:



protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

             int thisYear = Convert.ToInt32(System.DateTime.Now.Year);

            for (int i = 1999; i <= thisYear; i++)
            {
                ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));

            }
            ListItem lsitem = new ListItem("Select Year", "0");
            ddlYear.Items.Insert(0, lsitem);
        }
    }








3 comments:

  1. while converting to datatype int it through an exception so try this code to get the year value from DateTime

    DateTime today=System.DateTime.Now;
    int j=today.Year;

    ReplyDelete
    Replies
    1. Sorry for error... Now i have changed my code for converting datatype. use the above code it will help you...

      Delete