Click here to Skip to main content
15,883,802 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,

I would like to convert Date field to null while exporting to text file.
Default date is '01/01/1753'.

if (invoice.ContractDatetime = "01/01/1753")
{
invoice.ContractDatetime = "";
}

I tried the above. it throws the following errors

Error 1 Cannot implicitly convert type 'string' to 'System.DateTime'
Error 2 Cannot implicitly convert type 'System.DateTime' to 'bool'
Error 3 Cannot convert null to 'System.DateTime' because it is a value type

Thanks in Advance.
Posted
Updated 28-Jun-14 20:02pm
v2
Comments
PIEBALDconsult 29-Jun-14 1:55am    
So? What's stopping you?
Murugan.NM 29-Jun-14 2:01am    
Hi Sir,
if (invoice.ContractDatetime = "01/01/1753")
{
invoice.ContractDatetime = "";
}
I tried the above. it throws the following errors
Error 1 Cannot implicitly convert type 'string' to 'System.DateTime'
Error 2 Cannot implicitly convert type 'System.DateTime' to 'bool'
Error 3 Cannot convert null to 'System.DateTime' because it is a value type

I assume you mean that you want to write null if the date is same as default.

This is one way to do it.

C#
DateTime defaultDate = DateTime.Parse("01/01/1753");

DateTime someDate1 = new DateTime();
if (someDate1.Date == defaultDate.Date)
  Console.WriteLine("null");
else
 Console.WriteLine(someDate1.ToString());

DateTime someDate2 = DateTime.Parse("01/01/1753");
if (someDate2.Date == defaultDate.Date)
  Console.WriteLine("null");
else
  Console.WriteLine(someDate2.ToString());
 
Share this answer
 
Comments
Murugan.NM 29-Jun-14 2:43am    
Hi Sir,

I tried but not it passed null value
What is wrong with i tried the following
DateTime defaultdate = DateTime.Parse("1753-01-01");

DateTime ContractDatetime = new DateTime();
if (invoice.ContractDatetime.Date == defaultdate.Date)
{
Console.WriteLine("null");
}
else
{
Console.WriteLine(invoice.ContractDatetime.ToString());
}
George Jonsson 29-Jun-14 5:16am    
My code snippet was just an example.
How do you want the text file to look like in the different cases?
Test against sqldatetime.minvalue[^]
 
Share this answer
 

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



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