1

I am trying to get field value but keep getting using code,

if(listItem["myLookUpColumnName"] != null)

Exception

Value does not fall within the expected range.

What can i do to avoid this exception even though field name is alright.

  • Are you sure you are using the internal name of the field and not the display name. – Nadeem Yousuf Aug 28 '13 at 12:35
  • it is internal name of field – Mathematics Aug 28 '13 at 12:52
1

Check the List schema and see what name it has for the corresponding column which you are referring to in your code.

  • already did, its same – Mathematics Aug 28 '13 at 13:40
0

Lookup field is a field which links a child class to a parent class, it is an association kind, which allows a list item to reference another item from another list. However, reading and writing to a lookup field is different from a normal field. We need to use the class SPFieldLookupValue to achieve this.

Ex:

SPFieldLookupValue spv = new SPFieldLookupValue(listItem["LOOKUP_NAME"].ToString());
ddl.SelectedValue = spv.LookupValue.ToString(); // this line gets the value from the lookup field and I'm assigning it to a dropdown, you can change it according to your wish.

Use the Internal name of the Lookup field to access it.

  • thankyou but its a multiple value lookup – Mathematics Aug 28 '13 at 12:47
  • then try using // Get the predecessors. string rawvalue = item[SPBuiltInFieldId.Predecessors].ToString(); SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(rawvalue); – Karthik Jaganathan Aug 28 '13 at 13:17
  • what's Predecessors ? – Mathematics Aug 28 '13 at 13:30
0

It looks like the field name you're referring to doesn't exist in the item, even though it exists in the list.

With normal columns this can happen if the field isn't listed in the ViewsFields. With lookups this can also happen if the lookup field was added after the list item.

Here is my answer on a similar question, with more details: ArgumentException when trying to access SPListItem value

  • unfortunately your answer didn't helped, thanks tho – Mathematics Aug 28 '13 at 14:49
  • when i try to get SPFields of item, it does show the field i am trying to get value of – Mathematics Aug 28 '13 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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