title: Why do I get 'Index was outside the bounds of the array.' error?
body: <p>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. </p>
  
  <p>Upon using the the following code where manager is the lookup field in the Employee list, I get the error:</p>
  
  <blockquote>
    <p>Index was outside the bounds of the array.</p>
  </blockquote>
  
  <pre><code>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 = @"&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='ID' /&gt;&lt;Value Type='Counter'&gt;" + itemID + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;";
              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;
          }
      }
  </code></pre>
  
  <p>I am using a sharepoint list(employee) that contains 2 lookup columns(department and manager). The above code is to update the sharepoint list</p>
  
  <p>Kindly help! Thanks! :)</p>