'Test if null for a sharepoint column with C# in a dynamic field of the mapping tool layer2 cloud connector
I use the layer2 cloud connector synchronisation tool to synchronize between a sql database and a sharepoint database on the right side. When synching from right to left (from SP to SQL) in a datetime field "dl date" there can be empty values. I established a dynamic column in the tool, which should return "01.01.1753" if in SP "dl date" was empty or just the regular date if it was not empty. That's due to our SQL database, which doesn't accept empty values but has to have this weird "01.01.1753" as empty value. In the definition of the dynamic field I use the following C#-Code:
if(String.IsNullOrEmpty(NächstesFSKontrolldatum.ToString())) {
return DateTime.Parse("01.01.1753"); } else { return
NächstesFSKontrolldatum; }
But no matter how I compare the field "NächstesFSKontrolldatum" (aka "dl date") if it's null, it doesn't fire the part of the condition for beeing null.
When the tool finally writes just the date to sql there is an error which says can't put string '' into date field. I guess that always my second case of the condition fires and he tries to write just empty values into the database.
Because this code runs inside of the layer2 tool, I don't know how to debug or write to the console. Maybe you know or it's not possible?
Do you know, which comparison I have to make?
Andreas
Solution 1:[1]
I think is beter test if the 'NächstesFSKontrolldatum' can be parsed with a TryParse
DateTime testDate;
if(!DateTime.TryParse(NächstesFSKontrolldatum.ToString(), out testDate))
{
return DateTime.Parse("01.01.1753");
}
else
{
return testDate;
}
This way you're sure the value returned can be readed as a Date
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | J.Salas |
