为什么我得到的索引超出了数组的范围。'错误?

时间:2017-08-17 05:33:13

标签: c# asp.net gridview sharepoint sharepoint-2013

我正在使用asp.net网格在SharePoint列表上显示和执行CRUD操作。我无法更新列表中的值。

使用以下代码,其中manager是Employee列表中的查找字段,我收到错误:

  

索引超出了数组的范围。

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;
        }
    }

我正在使用包含2个查阅列(部门和经理)的共享点列表(员工)。上面的代码是更新sharepoint列表

请帮忙!谢谢! :)

1 个答案:

答案 0 :(得分:1)

看起来你的函数无法拆分id,可能是因为它不包含#。当您尝试访问不属于实际数组的数组中的位置时,“索引超出了数组的范围”。您正尝试访问索引1(第2个位置)。在这种情况下,它看起来不可用。

更好的是,尝试抓住那里来处理错误。