views:

29

answers:

3

I have the following helper method that returns the value from a field.

public static string GetValueFrom(SPListItem item, string fieldName)
        {
            string value = string.Empty;
            if (item.Fields.ContainsField(fieldName))
            {
                SPField field = item.Fields.GetField(fieldName);

                if (item[field.InternalName] != null)
                {
                    value = item[field.InternalName].ToString();
                }
            }
            return value;
        }

However for one Field (normal Choice Field) I am getting a ArgumentExecption on this line

if (item[field.InternalName] != null)

I am using

SPListItem item = list.GetItemById(itemId);

To get the item.

I cant find why I am getting the exception when I am checking to see if the field exists? Any ideas as to why I am getting this Exception for only one field.


Update.

When debugging

The call to GetField() returns the correct field object. Field.InternalName contains the correct Internal name of the field If I try and access the value using item["internal name of the field"] it still throws and exception for only this one field.

+1  A: 

Have you tried debugging? Questions you should answer (because we can't):

  • Is field a valid value, or null, after the call to GetField()?
  • If field is not null, what does field.InternalName actually return?
  • If field.InternalName returns a valid value, can you access it by hard-coding that value in the indexer? i.e. item["fieldInternalName"]

Finding that information may help you solve the problem yourself, but if it doesn't add it to your post so the community has a better chance of helping you.

CBono
Thanks for the post , I am updating my question.
JC Vivian
+1  A: 

Sometimes strange things happens and we do not have logical answer to those questions. Try by deleting the list and then creating the list again from scratch. DO NOT try to save it as template and DO NOT try to create the list from that template.

One possible reason of such type of ugly messages is that the security/permissions are not allowing to manipulate that field/column.

Another possible reason of such type of unwanted/unexpected messages is that when the field was created for the first time, its data type was different and later on it was changed to choice. Technically there should be no problem in doing so but sometimes we face odd behavior.

Azher Iqbal
A: 

Hi,

I do experienced this many a times. The reason for this is if you are logged-in as a non Admin Account(System Account) the default List View Lookup Threshold for the User is 8 for the lookup columns. i.e for the default view the user can access upto the 8 lookup fields only. If you change the List Throttling to >8 it will be resolved. But increasing this will degrade the performance.

Go to Central Admin >> Manage Web Applications >> Select the Web Application >> General Settings Dropdown >> Resource Throttling >> Change the "List View Lookup Threshold" to more than 8

Thanks, -Codename "Santosh"

CodenameSantosh