-1

I am using asp.net grid to display and perform CRUD operations on a SharePoint list. And I'm unable to update values to the list.

Upon using the the following code where manager is the lookup field in the Employee list, I get the error:

Index was outside the bounds of the array.

public void updateRow(string itemID, string firstName, string lastName, string age, string eAddress, string department, string manager, string gender, string salary)
    {
        ClientContext clientContext = new ClientContext("https://xyz.xyz.com/sites/xyz/TrainingSite/");

        try
        {
            SP.List oList = clientContext.Web.Lists.GetByTitle("Employees");
            clientContext.Load(oList);
            SP.CamlQuery camlQuery = new SP.CamlQuery();
            camlQuery.ViewXml = @"<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemID + "</Value></Eq></Where></Query>";
            SP.ListItemCollection itemInfo = oList.GetItems(camlQuery);
            clientContext.Load(itemInfo);
            clientContext.ExecuteQuery();
            foreach (SP.ListItem item in itemInfo)
            {
                if (itemID == item["ID"].ToString())
                {
                    item["Title"] = firstName;
                    item["Last_x0020_Name"] = lastName;
                    item["u5ib"] = age;
                    item["Address"] = eAddress;

                    //Department column
                    //item["Department"] = department;                        
                    FieldLookupValue deptItem = new FieldLookupValue();
                    //deptItem.LookupId = Convert.ToInt32(department); //department should be department list item ID because its a lookup ID                       
                    item["Department"] = deptItem;
                    department = department.Split('#')[0]; //throws index was outside the bounds of the array error
                    deptItem.LookupId = Convert.ToInt32(department);
                    item["Department"] = deptItem;

                    //Manager column
                    //item["Manager"] = manager;
                    FieldLookupValue mgrItem = new FieldLookupValue();
                    manager = manager.Split('#')[0];//throws index was outside the bounds of the array error
                    mgrItem.LookupId = Convert.ToInt32(manager);
                    item["Manager"] = mgrItem;

                    item["Gender"] = gender;
                    item["Salary"] = salary;
                    item.Update();
                    break;
                }
            }
            clientContext.ExecuteQuery();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

I am using a sharepoint list(employee) that contains 2 lookup columns(department and manager). The above code is to update the sharepoint list

Kindly help! Thanks! :)

Sanjana V
  • 125
  • 1
  • 3
  • 11
  • in what line do you get the exception? I assume on the second which means that when you split by `#` there is only one item in the array. (remember arrays as zero based). [Read here to understand why it is zero based](https://stackoverflow.com/a/45697227/6400526) – Gilad Green Aug 17 '17 at 05:40
  • error simply says that there are Zero elements in array, you are trying to access second element `manager.Split('#')[1]` – Dgan Aug 17 '17 at 05:41
  • show us some more code please. How do you declare manager, what does it look like and of what type is it? – Marco Aug 17 '17 at 06:05

1 Answers1

1

It looks like your function could not split the id, maybe because it does not contain #.  "Index was outside the bounds of the array" comes when you try to access a position in array that is not part of actual array. You are trying to access index 1 (2nd position). In this case, it does not look available.

Better still, put up a try catch there to handle the error.

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22