SPList Item get value - ArgumentException

ProdShare picture ProdShare · Oct 20, 2009 · Viewed 10.4k times · Source

I have an SPListItem and I have an array of column names. When I try to access the SPListItem values using the code below:

for(int i=0;i<arrColName.length;i++)
{
    string tempValue =  item[arrColName[i]].ToString();
    // Works fine in case the the specific column in the list item is not null
    // Argument exception - Values does not fall witing expected range
    // exception in case the value //is null
}

Answer

Baris Bikmaz picture Baris Bikmaz · Jun 8, 2012

I think that you used an SPQuery to get the list items and forgot to add the field into the viewfields property of SPQuery.

query.ViewFields = string.Format("<FieldRef Name=\"{0}\" Nullable=\"True\" />", mFieldName);

Usually when you test your program with the farm account the code will work, with normal users you get an ArgumentException.

Another problem/feature which causes ArgumentException is the new ListView Threshold. If th elist you try to access has too many items, this Exception is raised. A way to handle this is to increase the threshold with powershell for the list.