-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列表(员工) (部门和经理)。以上代码是更新共享点列表
请帮忙!谢谢! :)
你在哪一行得到异常?我假设第二个这意味着当你用'#'分割时,数组中只有一个项目。 (记得数组为零)。 [读这里了解为什么它是基于零](https://stackoverflow.com/a/45697227/6400526) –
错误只是说,有数组中的零元素,你正试图访问第二个元素'manager.Split( '#')[1]' –
向我们展示了更多代码。你如何申报经理,它是什么样子的,它是什么类型的? – Marco