0

I have written a little bit of code, but while trying to add a date with "null" as a resultset from a database, I get an error, that null is not date / time.

So it seems, I have an error with the following code snippet... My question is: how to set the datatype correctly, that a "null" dataset would not create an error?

As you can see, first of all I make a difference between some datatypes to create the right objects. But in this one case, for the date, I want to allow null objects.

Could you please help me?

case kdType.Numeric20:
nKSI = ktSI.CreateKeyword(Int32.Parse(""+row[column]));
break;
case kdType.AlphaNumeric:
nKSI = ktSI.CreateKeyword(""+row[column]);
break;
case kdType.Date:                           
nKSI = ktSI.CreateKeyword(DateTime.ParseExact(""+row[column], "dd.MM.yyyy HH:mm:ss", null));                
break;
case kdType.Currency:
nKSI = ktSI.CreateKeyword(Convert.ToDecimal(float.Parse(""+row[column], CultureInfo.CurrentCulture.NumberFormat)));
break;
case kdType.SpecificCurrency:
nKSI = ktSI.CreateKeyword(Convert.ToDecimal(float.Parse(""+row[column], CultureInfo.CurrentCulture.NumberFormat)));
break;

Christian
  • 13
  • 4
  • The default date in c# is 1/1/01 which is what the null constructor uses. Some application set the default to a date like 1/1/1980 (or other default). You cannot set a DateTime object to null in c# so you must use a real value. – jdweng Mar 30 '21 at 13:16
  • @Christan, if my answer below solved your problem, you can mark it as accepted answer – Gian Paolo Apr 11 '21 at 09:26

1 Answers1

1

You have to chek for db null value before trying to use DateTime.ParseExact

something like this

object dbValue = row[column]
if (dbValue == DbNull.Value)
{
//handle NULL somehow
}
else
{
.... =DateTime.ParseExact(dbValue.ToString(), ....);
}
Gian Paolo
  • 4,161
  • 4
  • 16
  • 34