Click here to Skip to main content
15,883,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I wanna pass string like "-" to Crystal Report when DateOfBirth is Null


i am using this code for pass date to crystal report

C#
 <pre>.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();


and this is my Class

C#
<pre>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; }
}


What I have tried:

i try pass "-" when the DateOfBirth is Null

using this
C#
DateOfBirth =Convert.ToString(u.DateOfBirth) == string.Empty ? DateTime.Parse("-") : u.DateOfBirth


but get this error

System.FormatException: 'String was not recognized as a valid DateTime.'
Posted
Updated 21-Sep-20 4:42am

You can only assign valid DateTime Values or NULL to Nullable<system.datetime> DateOfBirth.
You cannot assign a string to a DateTime variable.
You cannot parse '-' to DateTime.
 
Share this answer
 
Why not do something simpler, like:
C#
var neededString = DateOfBirth.HasValue ? DateOfBirth.ToString() :  "-";

Refer: Nullable<T>.HasValue Property (System) | Microsoft Docs[^]
Quote:
Gets a value indicating whether the current Nullable<t> object has a valid value of its underlying type.
 
Share this answer
 
v2
i am using this method and work fine

C#
DateOfBirth = u.DateOfBirth == null ? "-":u.DateOfBirth.Value.ToShortDateString()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900