1

I have some code like this:

        DataTable table = new DataTable(); // New data table.
        table.Columns.Add("ClientId", typeof(int));
        table.Rows.Add(1);
        table.Rows.Add(2);
        table.Rows.Add(3);
        table.Rows.Add(4);
        table.Rows.Add(DBNull.Value);
        table.Rows.Add(DBNull.Value);

        table.Columns.Add("Section", typeof(string), "IIF(ClientId=Null,'Generic','Client')");

Right now, ever row is being give the value of client, in essence the ClientId=Null is always returning false.

What am I doing wrong here?

I want it so that if the row value of ClientId is null then the Section value will be genereic, otherwise the Section value will be Client.

Any Suggestions?

1
  • Change ClientId=Null to ClientId is Null, if that is VBA then maybe ClientId is nothing probably works.
    – MJVC
    Jul 8, 2014 at 16:19

1 Answer 1

3

You could change your Expression to

 table.Columns.Add("Section", typeof(string), 
                    "IIF((ISNULL(ClientId,0)=0),'Generic','Client')");

Well, of course no value zero should be present in the ClientId column, otherwise choose another value guaranteed to not exists in the values (like -1)

Explanation

ISNULL(expression, replacement_value)

In case of NULL the return of ISNULL is the replacement value, then this value is compared with the fixed constant zero to produce the boolean result required by the IIF expression. If the comparison between the result of ISNULL and zero is true then the first string is selected as value for the Section column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.