Weird IF behavior in C#

I have this IF/ELSE in my code and it works perfectly:

     if (ID == 3)
          SelectedRow ["LocationID"] = System.DBNull.Value;
          SelectedRow ["LocationID"] = ID;

The equivalent Immediate IF statement bombs!  The problem seems to be that I'm trying to assign either an int or a null.  Something about C# doesn't like the fact that SelectedRow ["LocationID"] can be either value (but it works fine in the IF above, just not the Immediate IF).  Here's the Immediate IF statement that bombs:

     SelectedRow ["LocationID"] = (ID != 3) ? System.DBNull.Value : ID;

And here's the error message:

     Type of conditional expression can't be determined because there is no implicit conversion between 'System.DBNull' and 'int'

It's not a huge deal, because the first IF works fine, but I am really curious... does anyone know why the Immediate IF doesn't work, and/or how to correct the behavior (if possible)?

Who is Participating?
if you see carefully,
SelectedRow["LocationID"] is of the type object. That is true for all untyped datatables. and since both DBNull.Value as well as an integer are objects, they can be boxed as objects.
i think you have an example of implicit casting.

in your first if-else block  SelectedRow[""] is casted to DBNull type as well as to int type and i assume you'll have to do checking when you try getting values back from your SelectedRow[""] since casting from System.DBNull to anything else is not possible.

Thus, in your second if statement, you have a type collision since compiler doesn't know at the compile time to what type SelectedRow should be casted to and throws exception...

I don't think you can make it working having both DBNull and int types in the same statement.

first of two if statments you described above are not equal
in first If statment you have "if(ID == 3)"
and 2nd (immediate if statment) you have "if(ID != 3)

thus I am assuming that when you run the first if statment, it does not reaches the statment System.DBNull.Value instead it just goes to SelectedRow[Location ID] = ID statment.

However when you run the Immediate if, it goes to the System.DBNull.Value, as a result 2nd one bombs out.

maybe try putting
SelectedRow[Location ID] = System.DBNull.Value;
without any if statment just to test my theory, and see if it bombs out...


Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

no, it won't and doesn't need to reach the statement to do type-checking - it's all done during the compile time. If it was an error, it would give an error while compiling.
Hi bek,

 Yep it must be the same typecast, Object.Null is not an Int and Int is not an Object Type cast. Maybe 0 instead would be better.

SelectedRow ["LocationID"] = (ID != 3) ? (object)System.DBNull.Value : (object)ID;
bekAuthor Commented:
Sorry for the confusion, guys.  The immediate IF statement had a small typo.  Both IF statements actually said (ID == 3).  Both lines do compile, because the error is at compile-time.

b1xml2, your line of code works fine.  SelectedRow ["LocationID"] accepts an int, like this:

SelectedRow ["LocationID"] = 3;

But it also accepts an object, like in your example.  Any idea why it will work with both types?
the conditional operator ?: expects the data type when the expression is true or false to be of the same type.
bekAuthor Commented:

I was thinking '3' is a primitive.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.