1

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!

1 Answer 1

1

You should interchange the lines in your code.

FieldLookupValue deptItem = new FieldLookupValue();
deptItem.LookupId = Convert.ToInt32(department);  //department should be department list item ID because its a lookup ID
item["Department"] = deptItem;
item.Update();

Hope this helps to you :)

5
  • I still get that Input string was not in correct format error, when i debug it reads the value of the first lookup and throws this error @Nagababu
    – Sanjana V
    Commented Aug 14, 2017 at 11:32
  • what is department.Text value in your code. I mean it's like 'HR Department' or '1' (look up ID) Commented Aug 14, 2017 at 11:35
  • Its like 'HR Department' @Nagababu
    – Sanjana V
    Commented Aug 14, 2017 at 11:43
  • 1
    Ok, here you should provide the ID for LookupID like '1'. Verify the other properties of Department like ID or Value instead of Text. Commented Aug 14, 2017 at 11:46
  • that works well with department.ID, But how does update happen? I cannot hard code the id of the textboxes in edit mode and pass those id's to the objects.. That doesn't seem to be a good method! How else does update take place? @Nagababu
    – Sanjana V
    Commented Aug 16, 2017 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.