I have a SharePoint list containing 2 lookup columns (department and manager). How do I update values to SP list upon edit in gridview?
The following is the code for an update method :
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");
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(TextBoxEditDepartment.SelectedValue); // here instead of 1, you should provide corresponding Department list item ID
item["Department"] = deptItem;
deptItem.LookupId = Convert.ToInt32(department);
item.Update();
//Manager column
//item["Manager"] = manager;
FieldLookupValue mgrItem = new FieldLookupValue();
item["Manager"] = mgrItem;
mgrItem.LookupId = Convert.ToInt32(manager);
item.Update();
item["Gender"] = gender;
item["Salary"] = salary;
item.Update();
break;
}
}
clientContext.ExecuteQuery();
}
Following is the code for RowUpdating event:
protected void gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Omitted few lines for clarity
DropDownList department = (DropDownList)gridview.Rows[e.RowIndex].Cells[4].FindControl("ddlDepartment"); //department
DropDownList manager = (DropDownList)gridview.Rows[e.RowIndex].Cells[5].FindControl("ddlManager"); //manager
//Omitted few lines for clarity
updateRow(itemID.Text, firstName.Text, lastName.Text, age.Text, eAddress.Text, department.Text, manager.Text, gender.Text, salary.Text); // call update method
gridview.EditIndex = -1; //Turn the Grid to read only mode
BindGrid(); // Rebind GridView to reflect changes made
Response.Write("Update Seccessful!");
}
I get an error like :"Input string was not in the correct format" for the following lines :
FieldLookupValue deptItem = new FieldLookupValue();
item["Department"] = deptItem;
deptItem.LookupId = Convert.ToInt32(department);
item.Update();
Please provide a solution :) Thanks!