. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to run a new project. I do a couple of things like this:
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 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;
}
}
but in my program, I am getting the warning:
Index was outside the bounds of the array.
Can someone explain to me why the issue of the index was outside the bounds of the array happened? Where have I gone wrong? Thank you!
The cause: Your function may be not contain #, so it could not split the id. “Index was outside the bounds of the array” occurs 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 is not available.
Solution: Try to catch there to handle the error.
Your function cannot split the id. It may be because it doesn’t contain #. When you attempt to access an array position that isn’t part of the actual array, index was considered outside of the bounds. This is index 1. It does not appear to be available in this instance.
You can even try to catch the error by putting up a try catch.