I want to calculate an Age inside a repeater with an existing field.
Query to populate repeater:
var qryGetAllBeneficiaries = from p in dbRRSP.Person
join rt in dbRRSP.RelationshipType on p.RelationshipTypeId equals rt.RelationshipTypeId
where p.PlanId == qryPersonDetails.PlanId
orderby p.LastName
select new
{
BeneficiaryLastName = p.LastName,
BeneficiaryFirstName = p.FirstName,
BeneficiaryMiddleName = p.MiddleName,
BeneficiaryAka = p.Aka,
BeneficiaryBirthday = p.Birthdate,
BeneficiaryRelationshipToClient = rt.RelationshipTypeDescription
};
rptBeneficiary.DataSource = qryGetAllBeneficiaries;
rptBeneficiary.DataBind();
Repeater itself:
<asp:Repeater ID="rptBeneficiary" runat="server">
<HeaderTemplate>
<table>
<tr>
<td class="labels displayInput_noWidth">Last Name</td>
<td class="labels displayInput_noWidth">First Name</td>
<td class="labels displayInput_noWidth">Middle Name</td>
<td class="labels displayInput_noWidth">Aka</td>
<td class="labels displayInput_75w">Birthday</td>
<td class="labels displayInput_noWidth">Age</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryLastName") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryFirstName") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryMiddleName") %></td>
<td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryAka") %></td>
<td><%#DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString()).ToString("MM/dd/yyyy")%></td>
<td> <%(DateTime.Today.Subtract(DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString())).Days / 365).ToString()%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I figured out how to just format into a date string I like, but what I'm looking for is to subtract today's date from the birthday to get the age of each Beneficiary in the repeater.
I tried to do the following calculation:
<td> <%(DateTime.Today.Subtract(DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString())).Days / 365).ToString()%></td>
but it wouldn't recognize the DataBinder.Eval container and asked me to add another using statement: System.ComponentModel.Container. Can someone tell me what I'm missing? Thanks in advance.