1

The following is the code for update event and 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");
            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(1); //department should be department list item ID because its a lookup ID                       
                    item["Department"] = deptItem;

                    //Manager column
                    //item["Manager"] = manager;
                    FieldLookupValue mgrItem = new FieldLookupValue();                       
                    mgrItem.LookupId = Convert.ToInt32(1);//giving 1 displays only the first item of the list [issue]
                    item["Manager"] = mgrItem;

                    item["Gender"] = gender;
                    item["Salary"] = salary;
                    item.Update();
                    break;
                }
            }
            clientContext.ExecuteQuery();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    protected void gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Accessing Edited values from the GridView
        TextBox itemID = (TextBox)gridview.Rows[e.RowIndex].Cells[0].FindControl("VJ"); //ID
        TextBox firstName = (TextBox)gridview.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditFirstName"); //firstName
        TextBox lastName = (TextBox)gridview.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditLastName"); //lastName
        TextBox age = (TextBox)gridview.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditAge"); //age
        TextBox eAddress = (TextBox)gridview.Rows[e.RowIndex].Cells[4].FindControl("TextBoxEditAddress"); //eAddress

        //TextBox department = (TextBox)gridview.Rows[e.RowIndex].Cells[4].FindControl("ddlDepartment"); //department
        //TextBox manager = (TextBox)gridview.Rows[e.RowIndex].Cells[5].FindControl("ddlManager"); //manager

        DropDownList department = (DropDownList)gridview.Rows[e.RowIndex].Cells[5].FindControl("ddlDepartment"); //department
        DropDownList manager = (DropDownList)gridview.Rows[e.RowIndex].Cells[6].FindControl("ddlManager"); //manager
        TextBox gender = (TextBox)gridview.Rows[e.RowIndex].Cells[7].FindControl("TextBoxEditGender"); //gender
        TextBox salary = (TextBox)gridview.Rows[e.RowIndex].Cells[8].FindControl("TextBoxEditSalary"); //salary

        updateRow(itemID.Text, firstName.Text, lastName.Text, age.Text, eAddress.Text, department.ID, manager.ID, 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!");
    }

Please help with code. Kindly help! Thanks.

  • Any errors you got during grid operations? Have you tried to debug the code? – Dikesh Gandhi Aug 17 '17 at 4:05
  • I don't get any errors! I did debug the code @DikeshGandhi – Sanjana V Aug 17 '17 at 5:48
1

Can you place breakpoint in updateRow method and check if it is executing or not. Also if it is executing try to put another breakpoint in catch block. This will help you get exact error.

As from the code (also question tag), I guess you are trying CSOM operation on SharePoint Online. In order to get SharePoint online context you must need to supply credentials to client context object. I am pretty sure as of now, your code is throwing Access Denied error. In order to solve it, use one of the following code based on your environment.

string username = isSPOnline ? "abc@xyz.com" : "domain\\username";
string pwd = "abcd";
SecureString password = new SecureString();

foreach (char ch in pwd)
{
    password.AppendChar(ch);
}

if (isSPOnline)
{
    ctx.Credentials = new SharePointOnlineCredentials(userName, password);
}
else
{
    ctx.Credentials = new NetworkCredential(userName, password, domain);
}

I hope this will solve your issue.

  • I did put breakpoints at updateRow as well as the catch block. Execution is fine! and I don't require any credentials. Code doesn't throw Access Denied error. @ThinkB4Code – Sanjana V Aug 17 '17 at 5:52
  • Apart from this, I cant see any other issue with your code. Can you debug if you are getting items in itemInfo object? – ThinkB4Code Aug 17 '17 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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