null field of a retrieved value RRS feed

  • Question

  • hi, i used CRMservice to retrieve a value (a picklist). when it is not null. i can use

    account1.acno.Value to retrieve the value properly. however when it is empty(picklist doesn't select anything), it shows error. and i use whatever method (isnull, isnullspecify, etc), it is unable to exceuted. simply, i think the retrievemutiple retrieve null when that picklist didn't select anything (while string field wouldn't happened)

    thus, in which steps i should handle the null value?

    this is the code that try to put the retrieved value into datatable

                (BusinessEntity be in
                    DataRow tmp;
                    account de = (account)be;
                    string ids="";
                    Picklist ints = new Picklist();               
                       ints=  de.interestinnewproducts;  <== it shows error when the interestinnewproducts field selected nothing                 
                    tmp = tk.NewRow(); <== tk is datatable
                    tmp["INT"] = ints.Value;



    Wednesday, January 12, 2011 5:01 AM


  • Firstly, allocating the picklist variable is redundant:

    Picklist ints = new Picklist();

    Given that, in the very next line, you assign it to something else.  To eliminate the redundancy, simultaneously null check and assign a default (in the case where no value is chosen, try:

    int interestinnewproducts = (de.interestinnewproducts == null ? 0 : de.interestinnewproducts.Value);
    This tests for a null property; if so, it returns a default value of 0 (alter to suit), otherwise it returns the selected value.
    --pogo (pat)
    • Marked as answer by verystupidguy Wednesday, January 12, 2011 5:42 AM
    Wednesday, January 12, 2011 5:14 AM