Weird IF behavior in C#

Posted on 2005-05-01
Last Modified: 2008-01-09
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)?

Question by:bek
    LVL 21

    Expert Comment

    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.

    LVL 1

    Expert Comment

    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...


    LVL 21

    Expert Comment

    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.
    LVL 9

    Expert Comment

    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.

    LVL 23

    Expert Comment

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

    Author Comment

    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?
    LVL 23

    Expert Comment

    the conditional operator ?: expects the data type when the expression is true or false to be of the same type.
    LVL 23

    Accepted Solution

    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.

    Author Comment


    I was thinking '3' is a primitive.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C# Help from Top Experts

    Solve your toughest problems, fast.
    C# experts are online now and ready to help you.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now