2017-08-17 78 views
-1

我正在使用asp.net网格在SharePoint列表中显示和执行CRUD操作。而且我无法将值更新到列表中。为什么我的索引超出了数组的范围。错误?

在使用下面的代码,其中经理在员工列表中查找字段,我得到的错误:

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

我使用的是包含2个查找列的SharePoint列表(员工) (部门和经理)。以上代码是更新共享点列表

请帮忙!谢谢! :)

+0

你在哪一行得到异常?我假设第二个这意味着当你用'#'分割时,数组中只有一个项目。 (记得数组为零)。 [读这里了解为什么它是基于零](https://stackoverflow.com/a/45697227/6400526) –

+0

错误只是说,有数组中的零元素,你正试图访问第二个元素'manager.Split( '#')[1]' –

+0

向我们展示了更多代码。你如何申报经理,它是什么样子的,它是什么类型的? – Marco

回答

1

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

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

相关问题