0

I've scoured stack overflow for the answer to my problem but nothing matches my situation.

I've got a struct which represents the schema of data retrieved from a stored procedure. the data type for a boolean field is nullable, but when the value returned is null, I cannot assign the value to the list item.

Struct looks like this:

    public GuidelineSchema gl;
    public struct GuidelineSchema
    {
        public int id;
        public int GLtypeID;
        public int typeValue;
        public Nullable<bool> isInlineLayout;
        public int ResinGroupID;
        public int NotResinGroupID;
        public int ResinSubTypeID;
         .
         .
         .
    }

and I populate it here

using (SqlDataReader sdr = cmd.ExecuteReader())
{
    while (sdr.Read())
    {
        GuidelineSchema gl = new GuidelineSchema() { 
        id = sdr.IsDBNull(0) ? 0 : sdr.GetInt32(0), 
         GLtypeID =  sdr.IsDBNull(1) ? 0 :sdr.GetInt32(1),
         typeValue = sdr.IsDBNull(2) ? 0 : sdr.GetInt32(2),
         //isInlineLayout = sdr.IsDBNull(3) ? (bool?)null : sdr.GetBoolean(3),
         //isInlineLayout = (bool?)sdr.GetSqlBoolean(3),//.GetBoolean(3),
         //isInlineLayout = (Nullable<Boolean>)sdr.GetBoolean(3),// sdr.IsDBNull(3) ?  : sdr.GetBoolean(3),//sdr.IsDBNull(3) ? false :                                                                 
         //isInlineLayout = sdr.IsDBNull(3) ? (bool?)null : sdr.GetSqlBoolean(3),
         isInlineLayout = sdr.GetBoolean(3),
         ResinGroupID = sdr.IsDBNull(4) ? 0 : sdr.GetInt32(4),
         NotResinGroupID = sdr.IsDBNull(5) ? 0 : sdr.GetInt32(5),
         .
         .
         .

The problem occurs only when the db value is null. I've tried all the different checks, and casts, each time I get the error. I've read up on sql data types, nullable data types. It works if i set the value to true or false if it's null, but I actually need it to be null if it's null.

Anyone know how I can get this done and allow the value to be null when it is supposed to be?

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Josawalk
  • 43
  • 5
  • 1
    This should work `isInlineLayout = sdr.IsDBNull(3) ? (bool?)null : sdr.GetBoolean(3),`, what error are you getting? – Ivan Stoev Mar 22 '16 at 20:41
  • see answer over here http://stackoverflow.com/questions/5409936/casting-datareader-value-to-a-to-a-nullable-variable – rashfmnb Mar 22 '16 at 20:46
  • This did work. I had this code in place verbatim and it was not working, but when I came into work yesterday morning, I saw your comment, tried it one more time and it was fine. There must have been some extraneous syntactic obfuscator or something. Anyway, thanks! – Josawalk Mar 24 '16 at 14:31

1 Answers1

1

Try

isInlineLayout = sdr[3] as bool?

we can take advantage of the fact that the data reader will return DbNull.Value if the column value is null. The as operator will produce a null if the value cannot be cast to bool?, which DbNull cannot.

user957902
  • 3,010
  • 14
  • 18