none
Assigning Person/Group column type value from one list to another list.

    Question

  • i am assigning value to a user column from a user column in a  list to another list using the following code

    item["Username"] = CurrentListItem["Username"].ToString();

    The code works fine when there is a value in CurrentListItem.But when there is no value in CurrentListItem it throws an null value exception.Any pointers on how to handle this will be helpful.

    Wednesday, January 01, 2014 2:20 PM

Answers

  • Then simply use this

    CurrentListItem["Username"] != null


    Amit

    • Marked as answer by SPDEVkktt Wednesday, January 01, 2014 3:06 PM
    Wednesday, January 01, 2014 2:57 PM

All replies

  • Can you not check if the value is null before assigning value?

    if(!String.IsNullOrEmpty(CurrentListItem["Username"].ToString())
    {
        item["Username"] = CurrentListItem["Username"].ToString();
    }


    Amit

    Wednesday, January 01, 2014 2:26 PM
  • Hi Amit,

    i tried the way you told but when it comes to the condition in if loop String.IsNullOrEmpty(CurrentListItem["Username"].ToString()) itselfs it throws an error object reference not set to an instance,and it goes to catch block.

        

     if (!String.IsNullOrEmpty(CurrentListItem["Username"].ToString()))
                            {
                                item["Username"] = CurrentListItem["Username"].ToString();
                            }  

    Thanks and regards


    Wednesday, January 01, 2014 2:50 PM
  • Then simply use this

    CurrentListItem["Username"] != null


    Amit

    • Marked as answer by SPDEVkktt Wednesday, January 01, 2014 3:06 PM
    Wednesday, January 01, 2014 2:57 PM
  • thanks amit that worked :)
    Wednesday, January 01, 2014 3:06 PM