Hi Dw

I'm create an application that register a user and now because we accept even oldest people we have to include their years in a dropdownbox so I'm not sure if this is possible but I do think it is possible it just that I don't have a clue how to start it, I want on form load the form count(not just counting but add the years starting let say from 1930 to current year automatically and add starting from 1930 to date to a dropdownbox so that a user can be able to select his/her year of birth) because typing each year manually on the codes will produce a long codes and not to mention the time it will take to complete. Anyone has an idea on how I can achieve this or references will be appreciated.

Dani AI

Generated

Nice work — a loop is the right idea and ’s suggestion to consider a date control is sensible for full DOB input. To build on those posts: populate the dropdown once (not on every postback), present years in the order your users will find easiest (recent-first is usually friendlier), and validate the selected year server-side so typos or unexpected values are caught.

A compact, robust pattern for ASP.NET (Page_Load) that binds a list of years descending:

If Not IsPostBack Then
    Dim startYear As Integer = 1930
    Dim endYear As Integer = DateTime.Now.Year
    Dim years = Enumerable.Range(startYear, endYear - startYear + 1) _
                          .Select(Function(y) y.ToString()) _
                          .Reverse() _
                          .ToList()
    DropDownList13.DataSource = years
    DropDownList13.DataBind()
    DropDownList13.Items.Insert(0, New ListItem("-- Select year --", ""))
End If

Notes and quick tips:

  • For WinForms use a ComboBox and the same generation logic inside the Form_Load event (only run once).
  • Always validate the selected value (Integer.TryParse and check startYear <= value <= DateTime.Now.Year). Optionally reject implausible ages (e.g., >130) to catch mistakes.
  • If you expect to accept very old birth years, pick a startYear accordingly and ensure any date controls or DB types you use can represent those dates.
  • If you want exact DOB instead of just year, use a date control (as suggested) so day/month are captured and you avoid post-selection ambiguity.

That gives a clean UX and keeps the code maintainable while covering edge cases.

Recommended Answers

All 3 Replies

I've just managed to solve this problem by playing around with loop.

Dim y As Long

For y = 1930 To Year(Now)
DropDownList13.Items.Add(y)
Next

Thanks ddanbe this was very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.