I would like it to be calculated here is what I have tried:
public int AgeCalc
{
get
{
DateTime now = DateTime.Today;
int agecalc = now.Year - DoB.Year;
if (DoB > now.AddYears(-Age))
Age--;
return Age;
}
}
but I get this error:
'-' cannot be applied.
I have a column in my database table called Age
. What I want it to do is when a date is selected in the DoB
(Date of Birth) column, it calculates the age of the person.
This is my T-SQL code:
CREATE TABLE [dbo].[Students]
(
[StudentId] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
[FirstName] nvarchar(200) NOT NULL,
[LastName] nvarchar(400) NOT NULL,
[DoB] datetime NULL,
[Age] int NOT NULL,
[Gender] char(10) NULL CHECK (Gender = 'Male' OR Gender = 'Female' OR Gender = 'Other'),
[ParentOrGuardian] nvarchar(400),
[PaymentEmail] varchar(255) NOT NULL,
[ContactNumber] char(10) NULL
)
This is my model class in C#:
namespace MusicDataApplication.Models
{
using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
public partial class Student
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Student()
{
this.Lessons = new HashSet<Lesson>();
}
public int StudentId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Birthday")]
[DataType(DataType.Date)]
public Nullable<System.DateTime> DoB { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
[Display(Name = "Parent/ Guardian")]
public string ParentOrGuardian { get; set; }
[Display(Name = "Payement Email")]
[DataType(DataType.EmailAddress)]
public string PaymentEmail { get; set; }
[Display(Name = "Contact Number")]
public string ContactNumber { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Lesson> Lessons { get; set; }
}
}