0

On clicking 'edit' button, the row is transformed into textboxes. But on changing some value and hitting the 'update' button, I get an error like "Invalid data has been used to update the list item. The field you are trying to update may be read only".

I also have 2 lookup columns(department and manager).

Kindly help me with a solution :)

The following is the code for RowUpdating event:

protected void gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Accessing Edited values from the GridView
        TextBox itemID = (TextBox)gridview.Rows[e.RowIndex].Cells[8].FindControl("VJ"); //ID
        TextBox firstName = (TextBox)gridview.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditFirstName"); //firstName
        TextBox lastName = (TextBox)gridview.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditLastName"); //lastName
        TextBox age = (TextBox)gridview.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditAge"); //age
        TextBox eAddress = (TextBox)gridview.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditAddress"); //eAddress
        TextBox department = (TextBox)gridview.Rows[e.RowIndex].Cells[4].FindControl("TextBoxEditDepartment"); //department
        TextBox manager = (TextBox)gridview.Rows[e.RowIndex].Cells[5].FindControl("TextBoxEditManager"); //manager
        TextBox gender = (TextBox)gridview.Rows[e.RowIndex].Cells[6].FindControl("TextBoxEditGender"); //gender
        TextBox salary = (TextBox)gridview.Rows[e.RowIndex].Cells[7].FindControl("TextBoxEditSalary"); //salary

        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!");
    }

Code block for update :

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://sp13appstoredev.xyz.com/sites/DevApps/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 (it is a look up column)
                    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();

                    //item["Department"] = department;
                    item["Manager"] = manager; //This is also a lookup column
                    item["Gender"] = gender;
                    item["Salary"] = salary;
                    item.Update();
                    break;
                }
            }
            clientContext.ExecuteQuery();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

Html for department and manager(which are lookup columns) :

            <asp:TemplateField HeaderText="Department">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBoxEditDepartment" runat="server" Text='<%# Eval("TextBoxEditDepartment") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="LabelDepartment" runat="server" Text='<%# Eval("TextBoxEditDepartment") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBoxDepartment" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Manager">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBoxEditManager" runat="server" Text='<%# Eval("TextBoxEditManager") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="LabelManager" runat="server" Text='<%# Eval("TextBoxEditManager") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBoxManager" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

And how do I add dropdowns for the above Lookups?

0

Use the ID of the lookup target, For ex:

 using(SPSite site = new SPSite("http://test.dev.com"))
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Suppliers"];
        SPListItem item = list.AddItem();

        item[SPBuiltInFieldId.Title] = "Test";
        item["Product"] = 1; // Use the ID; get it from the list

        item.Update();
    }

In this case, I have a list called Suppliers which has a field called Product that is a lookup to a list called Products. You can use the ID of the lookup target in either integer form or in string form ("1").

I've hardcoded it just to provide an example. You'll need to do a separate lookup to the target list to get the value.

| improve this answer | |
  • Is there any way to do it using CSOM? @Muskan – Sanjana V Aug 14 '17 at 10:29
-1

Only one thing I can say that u r lucky to work on ssom, as in my company we don't have access to server. so whatever we are supposed to do only with OOB. BTW you look cute in ur picture

| improve this answer | |
  • this is an answer to the question? @M.Qassas – SharePointer Aug 11 '17 at 8:48

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.