Hi Appache
Refer below updated sample.
HTML
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:DropDownList ID="ddlYears" runat="server" Width="100">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlMonths" runat="server" Width="100">
<asp:ListItem Text="January" Value="1" />
<asp:ListItem Text="February" Value="2" />
<asp:ListItem Text="March" Value="3" />
<asp:ListItem Text="April" Value="4" />
<asp:ListItem Text="May" Value="5" />
<asp:ListItem Text="June" Value="6" />
<asp:ListItem Text="July" Value="7" />
<asp:ListItem Text="August" Value="8" />
<asp:ListItem Text="September" Value="9" />
<asp:ListItem Text="October" Value="10" />
<asp:ListItem Text="November" Value="11" />
<asp:ListItem Text="December" Value="12" />
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlDates" runat="server" Width="100">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlYears2" runat="server" Width="100">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlMonths2" runat="server" Width="100">
<asp:ListItem Text="January" Value="1" />
<asp:ListItem Text="February" Value="2" />
<asp:ListItem Text="March" Value="3" />
<asp:ListItem Text="April" Value="4" />
<asp:ListItem Text="May" Value="5" />
<asp:ListItem Text="June" Value="6" />
<asp:ListItem Text="July" Value="7" />
<asp:ListItem Text="August" Value="8" />
<asp:ListItem Text="September" Value="9" />
<asp:ListItem Text="October" Value="10" />
<asp:ListItem Text="November" Value="11" />
<asp:ListItem Text="December" Value="12" />
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlDates2" runat="server" Width="100">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button Text="Calculate" runat="server" OnClick="CalculateAge" />
</td>
</tr>
<tr>
<td>You are :
</td>
<td colspan="3" align="center">
<asp:TextBox runat="server" ID="txtAge" Width="220" />
</td>
</tr>
</table>
</form>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dtYear = new DataTable();
dtYear.Columns.Add("Year", typeof(int));
for (int i = 1990; i <= DateTime.Now.Year; i++)
{
dtYear.Rows.Add(i);
}
ddlYears.DataTextField = "Year";
ddlYears.DataValueField = "Year";
ddlYears.DataSource = dtYear;
ddlYears.DataBind();
DataTable dtDate = new DataTable();
dtDate.Columns.Add("Date", typeof(int));
for (int i = 1; i <= 31; i++)
{
dtDate.Rows.Add(i);
}
ddlDates.DataTextField = "Date";
ddlDates.DataValueField = "Date";
ddlDates.DataSource = dtDate;
ddlDates.DataBind();
DataTable dtYear2 = new DataTable();
dtYear2.Columns.Add("Year", typeof(int));
for (int i = 1990; i <= DateTime.Now.Year; i++)
{
dtYear2.Rows.Add(i);
}
ddlYears2.DataTextField = "Year";
ddlYears2.DataValueField = "Year";
ddlYears2.DataSource = dtYear2;
ddlYears2.DataBind();
DataTable dtDate2 = new DataTable();
dtDate2.Columns.Add("Date", typeof(int));
for (int i = 1; i <= 31; i++)
{
dtDate2.Rows.Add(i);
}
ddlDates2.DataTextField = "Date";
ddlDates2.DataValueField = "Date";
ddlDates2.DataSource = dtDate2;
ddlDates2.DataBind();
}
}
protected void CalculateAge(object sender, EventArgs e)
{
int year = int.Parse(ddlYears.SelectedValue);
int month = int.Parse(ddlMonths.SelectedValue);
int date = int.Parse(ddlDates.SelectedValue);
int year2 = int.Parse(ddlYears2.SelectedValue);
int month2 = int.Parse(ddlMonths2.SelectedValue);
int date2 = int.Parse(ddlDates2.SelectedValue);
DateTime dob = new DateTime(year, month, date, 0, 0, 0);
DateTime futureDate = new DateTime(year2, month2, date2, 0, 0, 0);
TimeSpan ts = futureDate - dob;
DateTime age = DateTime.MinValue + ts;
int years = age.Year - 1;
int months = age.Month - 1;
int days = age.Day - 1;
txtAge.Text = String.Format("{0} year/s {1} month/s {2} day/s old", years, months, days);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dtYear As DataTable = New DataTable()
dtYear.Columns.Add("Year", GetType(Integer))
For i As Integer = 1990 To DateTime.Now.Year
dtYear.Rows.Add(i)
Next
ddlYears.DataTextField = "Year"
ddlYears.DataValueField = "Year"
ddlYears.DataSource = dtYear
ddlYears.DataBind()
Dim dtDate As DataTable = New DataTable()
dtDate.Columns.Add("Date", GetType(Integer))
For i As Integer = 1 To 31
dtDate.Rows.Add(i)
Next
ddlDates.DataTextField = "Date"
ddlDates.DataValueField = "Date"
ddlDates.DataSource = dtDate
ddlDates.DataBind()
Dim dtYear2 As DataTable = New DataTable()
dtYear2.Columns.Add("Year", GetType(Integer))
For i As Integer = 1990 To DateTime.Now.Year
dtYear2.Rows.Add(i)
Next
ddlYears2.DataTextField = "Year"
ddlYears2.DataValueField = "Year"
ddlYears2.DataSource = dtYear2
ddlYears2.DataBind()
Dim dtDate2 As DataTable = New DataTable()
dtDate2.Columns.Add("Date", GetType(Integer))
For i As Integer = 1 To 31
dtDate2.Rows.Add(i)
Next
ddlDates2.DataTextField = "Date"
ddlDates2.DataValueField = "Date"
ddlDates2.DataSource = dtDate2
ddlDates2.DataBind()
End If
End Sub
Protected Sub CalculateAge(ByVal sender As Object, ByVal e As EventArgs)
Dim year As Integer = Integer.Parse(ddlYears.SelectedValue)
Dim month As Integer = Integer.Parse(ddlMonths.SelectedValue)
Dim date1 As Integer = Integer.Parse(ddlDates.SelectedValue)
Dim year2 As Integer = Integer.Parse(ddlYears2.SelectedValue)
Dim month2 As Integer = Integer.Parse(ddlMonths2.SelectedValue)
Dim date2 As Integer = Integer.Parse(ddlDates2.SelectedValue)
Dim dob As DateTime = New DateTime(year, month, date1, 0, 0, 0)
Dim futureDate As DateTime = New DateTime(year2, month2, date2, 0, 0, 0)
Dim ts As TimeSpan = futureDate - dob
Dim age As DateTime = DateTime.MinValue + ts
Dim years As Integer = age.Year - 1
Dim months As Integer = age.Month - 1
Dim days As Integer = age.Day - 1
txtAge.Text = String.Format("{0} year/s {1} month/s {2} day/s old", years, months, days)
End Sub
Screenshot