0

I am assigning values to a model. Some of these values can be null. I have tried several methods to handle DBNull values but still getting errors

 dPbtbud.Xfer = dataFromFile.Tables[0].Rows[i][j++] == DBNull.Value ? 
      0.0 : 
      Convert.ToDouble(dataFromFile.Tables[0].Rows[i][j++]);

Also tried

 dPbtbud.Xfer = dataFromFile.Tables[0].Rows[i][j++] is DBNull ? 
     0.0 : 
     Convert.ToDouble(dataFromFile.Tables[0].Rows[i][j++]);

Non seem to handle null values

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Have you tried: `dataFromFile.Tables[0].Rows[i][j++] == null`? – Stefan Jul 12 '19 at 21:09
  • @Stefan Yeah just tried. Getting same error "Object cannot be cast from DBNull to other types" – masksoverflow Jul 12 '19 at 21:14
  • Where is this data coming from? It looks like it has been read from a file. Is it string data? Than it should be `string.IsNullOrWhiteSpace(dataFromFile.Tables[0].Rows[i][j++]) ? 0.0 : ..` – Stefan Jul 12 '19 at 21:16
  • Unless, it's value is actual `"NULL"` or `"NILL"` or something like that. – Stefan Jul 12 '19 at 21:16
  • Possible duplicate of [Object cannot be cast from DBNull to other types](https://stackoverflow.com/questions/6098646/object-cannot-be-cast-from-dbnull-to-other-types) – Fabio Jul 12 '19 at 21:21
  • Tried that as well...Yeah the data is coming from an excel file exported to a dataset – masksoverflow Jul 12 '19 at 21:22
  • @Fabio except I have tried that soln in that question as you can see and still having problems – masksoverflow Jul 12 '19 at 21:23
  • @Fabio I scaffolded the model from a database, messing with the nullability won't mess with the database? – masksoverflow Jul 12 '19 at 21:26
  • @masksoverflow, can you please add to the question what you tried and what "_Non seem to handle null values_" mean in your tries, what exception you get for example. – Fabio Jul 12 '19 at 21:27
  • @Fabio nevermind, the property of Xfer is already nullable – masksoverflow Jul 12 '19 at 21:28
  • @masksoverflow, if column in database is nullable then should be ok. – Fabio Jul 12 '19 at 21:28
  • @Fabio I can't do if and else on it. As in if(!(dataFromFile.Tables[0].Rows[i][j++]) is DBNull)) Because I am doing this for a couple of values and its going to get really ugly – masksoverflow Jul 12 '19 at 21:31
  • 2
    You realise that the `j++` executes up to twice, right? So when it is not null, you are fetching the next row (which now may be null). – GSerg Jul 12 '19 at 21:32
  • Since `Xfer` is nullable double, just do `dPbtbud.Xfer = dataFromFile.Tables[0].Rows[i].Field[j++]` – Fabio Jul 12 '19 at 21:35
  • @GSerg Yeah I do realize that. But I think you may be right as in its looking for a value that may now be null – masksoverflow Jul 12 '19 at 21:43

1 Answers1

0

My suggestion is to create an extension method to check null and DBNull :

namespace Helper
{
 public static class ConvertEX1
    {
        public static object IsNull<T>(this object obj, T def) where T : struct
        {
            if (obj == null || obj == DBNull.Value)
                return (T)Convert.ChangeType(def, typeof(T));
            return (T)Convert.ChangeType(obj, typeof(T));
        }
    }
}

Don't forget to import the namespace in every .cs file you use your function:

using Helper;
...


dPbtbud.Xfer = (double)dataFromFile.Tables[0].Rows[i][j++].IsNull(0.0);
nAviD
  • 2,784
  • 1
  • 33
  • 54
  • This extension is redundant for OP case, `DataRow` already has extension method `.Field` which will return correct type. – Fabio Jul 13 '19 at 09:28