You just need to know how to enter null :
roleID = readerRole.GetInt16(0) == 0 ? (int?)null : (int)readerRole.GetInt16(0);
Personally, I would cache the value, though:
int tmp = readerRole.GetInt16(0); // implicit widening to int here roleID = tmp == 0 ? (int?)null : tmp;
Although I also questioned the wisdom of turning 0 into null - it's better to use IsDBNull - something like:
if(reader.IsDBNull(0)) { roleID = null; } else { roleID = (int)readerRole.GetInt16(0); }
Marc Gravell Nov 07 '12 at 8:26 2012-11-07 08:26
source share