0
votes

I am trying to get a column value from a SharePoint list and populate it to an ASP text box. I used SPQuery to filter and get the value. I even mentioned view fields and increased the List View Lookup Threshold. But when I am trying to assign the text box value with the column field, I am getting this exception:

Argument exception was unhandled by user- value does not fall within the expected range.

Is there any workaround for this? Code sample:

SPQuery qrySeriesDesc = new SPQuery();
qrySeriesDesc.Query = "<Where><Eq><FieldRef Name='Series'/><Value Type='Text'>" + SeriesNumber + "</Value></Eq></Where>";
qrySeriesDesc.ViewFields = "<FieldRef Name='Series Description'/>";
SPListItemCollection itemCol = list.GetItems(qrySeriesDesc);
foreach (SPListItem item in itemCol)
{
    if (item != null)
    {
        tboxSeriesDescription.Text = item["Series Description"].ToString();            
    }
}

I am getting the mentioned exception at:

tboxSeriesDescription.Text = item["Series Description"].ToString();
2

2 Answers

0
votes

Try to get it from field, not from item:

        SPField fieldSerDesc = item.Fields.GetFieldByInternalName("SeriesDescription"); //internal name of your fields. Usually eq StaticName.
        tboxSeriesDescription.Text = item[fieldSerDesc.Id].ToString();

Or, if your field is lookup for example, you can do it like this:

        SPFieldLookup fieldSerDesc = (SPFieldLookup)item.Fields.GetFieldByInternalName("SeriesDescription");
        tboxSeriesDescription.Text = fieldSerDesc.GetFieldValueAsText(item[fieldSerDesc.Id]);
    }
0
votes

You get the error because the field do not exist or is misspelled.

Please note that if you select a column that does not exist SharePoint does not raise any error.

Try to check the field's name using a tool like Sharepoint Manager and use ALWAYS the internal name