Search code examples
c#entity-frameworkcrystal-reports

Pass date as String to Crystal Report Using Entity Framework


I want to pass a string like "-" to Crystal Report when DateOfBirth is Null, but I get this error:

System.FormatException: 'String was not recognized as a valid DateTime.'

When using this code:

 .Select(u => new
{

 u.EmployeeCode,
 u.EmployeeName,
 JopName = string.IsNullOrEmpty(u.JopName) ? "-" : u.JopName,
 Date_Hiring = u.Date_Hiring.GetValueOrDefault(),
 AdministrationName = string.IsNullOrEmpty(u.AdministrationName) ? "-" : u.AdministrationName,
 DepartmentName = string.IsNullOrEmpty(u.DepartmentName) ? "-" : u.DepartmentName,
 PranchName = string.IsNullOrEmpty(u.PranchName) ? "-" : u.PranchName,
 DateOfBirth =Convert.ToString(u.DateOfBirth) == string.Empty ? DateTime.Parse("-") : u.DateOfBirth
})
 .ToList();

This is my Class :

public partial class SR1_Result
 {
   public int EmployeeCode { get; set; }
   public string EmployeeName { get; set; }
   public string JopName { get; set; }
   public Nullable<System.DateTime> Date_Hiring { get; set; }
   public Nullable<double> Nat_Salary { get; set; }
   public string AdministrationName { get; set; }
   public string DepartmentName { get; set; }
   public string PranchName { get; set; }
   public string MobilePersonalNo { get; set; }
   public string MobileWorkNo { get; set; }
   public Nullable<System.DateTime> DateOfBirth { get; set; }
}

Solution

  • Not tested, but can't it be something like this:

    DateOfBirth = u.DateOfBirth == null ? "-" : u.DateOfBirth.Value.ToString("<your date format here>")