I'm using VWD 2010 Express with VB and want to display age, calculated from Date of birth from an SQL datasource, in a gridview column. I set up a templated field to hold the age and a public function to calculate the age from the date of birth(I got these
from web forums). Code shown below.
This works fine where the date of birth field holds a value but fails(on the Eval statement) when the date of birth is NULL. I'm happy to show age as zero for a NULL date of birth but cannot see how to handle the NULL value.
Can anyone advise how I can handle the NULL datetime field?
Public Function ageCalc(ByVal DateIn As Date) As Integer
Dim Age As Int16
Dim result As Integer
Dim rema As Integer
Dim startdate As Date = DateIn
If DateIn.ToString <> "" Then
Dim totalmonths As Integer
totalmonths = DateDiff(DateInterval.Month, startdate, Now)
result = Math.DivRem(totalmonths, 12, rema)
If startdate.Month = Now.Month Then
Dim eldia As Integer = startdate.Day
If eldia > Now.Day Then
result -= 1
End If
End If
End If
Age = result
I've been looking at "widening conversions" and decided that my problem was that the agecalc resulted in an integer whereas the "" potentially gave a text field(See if statement below).
I have now amended the statement in the template to be as shown below and it works fine. As the original version came from ryanbesko I've decided his reponse decides to be marked as the answer.
RugbyJohn
Member
67 Points
114 Posts
Null datetime field and age calculation in gridview
Feb 28, 2012 03:30 PM|LINK
Hi all
I'm using VWD 2010 Express with VB and want to display age, calculated from Date of birth from an SQL datasource, in a gridview column. I set up a templated field to hold the age and a public function to calculate the age from the date of birth(I got these from web forums). Code shown below.
This works fine where the date of birth field holds a value but fails(on the Eval statement) when the date of birth is NULL. I'm happy to show age as zero for a NULL date of birth but cannot see how to handle the NULL value.
Can anyone advise how I can handle the NULL datetime field?
==================
Template field statement
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# ageCalc(Eval("Date_of_Birth")) %>'></asp:Label>
</ItemTemplate>
==================
Public function
Public Function ageCalc(ByVal DateIn As Date) As Integer
Dim Age As Int16
Dim result As Integer
Dim rema As Integer
Dim startdate As Date = DateIn
If DateIn.ToString <> "" Then
Dim totalmonths As Integer
totalmonths = DateDiff(DateInterval.Month, startdate, Now)
result = Math.DivRem(totalmonths, 12, rema)
If startdate.Month = Now.Month Then
Dim eldia As Integer = startdate.Day
If eldia > Now.Day Then
result -= 1
End If
End If
End If
Age = result
Return Age
End Function
==================
ryanbesko
Contributor
3561 Points
619 Posts
Re: Null datetime field and age calculation in gridview
Feb 28, 2012 03:57 PM|LINK
Text='<%# If(Not IsDBNull(Eval("Date_of_Birth")), AgeCalc(Eval("Date_of_Birth")), "")%>'
RugbyJohn
Member
67 Points
114 Posts
Re: Null datetime field and age calculation in gridview
Feb 29, 2012 06:50 AM|LINK
That lookedd great but it produces a build error(See below)
Cannot infer a common type for the second and third operands of the 'If' operator. One must have a widening conversion to the other.
I don't understand what widening conversion means and thus how to do this.
Any advice?
vinay13mar
Star
7756 Points
1626 Posts
Re: Null datetime field and age calculation in gridview
Feb 29, 2012 06:55 AM|LINK
Hi,
check
<asp:TemplateField HeaderText="Date Processed" SortExpression="DateProcessed"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# ((DateTime)Eval("DateProcessed")) == DateTime.MinValue ? "" : Eval("DateProcessed", "{0:d}") %>'> </asp:Label> </ItemTemplate> </asp:TemplateField>this
V.K.Singh
RugbyJohn
Member
67 Points
114 Posts
Re: Null datetime field and age calculation in gridview
Feb 29, 2012 03:29 PM|LINK
Vinay
Thanks for the reply but I do not understand how this applies to my problem?
RugbyJohn
Member
67 Points
114 Posts
Re: Null datetime field and age calculation in gridview
Feb 29, 2012 03:43 PM|LINK
I've been looking at "widening conversions" and decided that my problem was that the agecalc resulted in an integer whereas the "" potentially gave a text field(See if statement below).
Text='<%# If(Not IsDBNull(Eval("Date_of_Birth")), AgeCalc(Eval("Date_of_Birth")), "")%>'
I have now amended the statement in the template to be as shown below and it works fine. As the original version came from ryanbesko I've decided his reponse decides to be marked as the answer.
Text='<%# If(Not IsDBNull(Eval("Date_of_Birth")), AgeCalc(Eval("Date_of_Birth")), 0)%>'
Thanks ryan