GridView with ObjectDatasource UpdateMethod

Henk Mollema picture Henk Mollema · Jun 17, 2013 · Viewed 11k times · Source

I have an ASP.NET WebForms page containing an ASPxGridView and an ObjectDataSource:

<dx:ASPxGridView ID="gvEmployees" runat="server"
    AutoGenerateColumns="False" DataSourceID="objDsEmployees"
    KeyFieldName="EmployeeId">
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0">
            <EditButton Visible="True" />
        </dx:GridViewCommandColumn>
        <dx:GridViewDataTextColumn FieldName="EmployeeId" VisibleIndex="1" />
        <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2" />
        <dx:GridViewDataTextColumn FieldName="Email" VisibleIndex="3" />
        <dx:GridViewDataTextColumn FieldName="Telephone" VisibleIndex="5" />
    </Columns>
</dx:ASPxGridView>
<asp:ObjectDataSource ID="objDsEmployees"
    runat="server"
    ConflictDetection="CompareAllValues"
    DataObjectTypeName="MySite.Data.Models.Employee"
    DeleteMethod="Delete"
    OldValuesParameterFormatString="original{0}"
    InsertMethod="Insert"
    SelectMethod="GetAll"
    TypeName="MySite.Services.EmployeeService"
    UpdateMethod="Update" />

The Employee model contains the following properties:

public class Employee
{
    public int EmployeeId { get; set; }

    public string Name { get; set; }

    public string Email { get;

    public string Password { get; set; }

    public string Telephone { get; set; }
}

The ObjectDataSource calls the Update method in the service layer:

public void Update(Employee employee, Employee originalEmployee)
{
    _db.Employees.Attach(originalEmployee);
    _db.Entry(originalEmployee).CurrentValues.SetValues(employee);
    _db.SaveChanges();
}

When the Update method is called, the parameters employee and originalEmployee only contain the properties used as columns in the ASPxGridView and the other properties (Password for example) are null.

Is there a way I can preserve these values somewhere? By the way, I use Entity Framework for data access and DevExpress for the GridView.

Answer

Henk Mollema picture Henk Mollema · Oct 5, 2013

You can also solve this by setting ConflictDetection="CompareAllValues" in the ObjectDataSource:

<asp:ObjectDataSource ID="objDsEmployees"
    runat="server"
    DeleteMethod="Delete"
    InsertMethod="Insert"
    SelectMethod="GetAll"
    UpdateMethod="Update"
    DataObjectTypeName="App.Core.Domain.Employee"
    TypeName="App.Core.Services.EmployeeService"
    OldValuesParameterFormatString="original{0}"
    ConflictDetection="CompareAllValues" />

Then in the Update method in EmployeeService we fetch the original employee first by its id, then we merge this original employee with the updated employee. This makes sure that not updated properties don't become null - although it requires an extra call to the database:

public void Update(Employee originalEmployee, Employee employee)
{
    // Get the original employee by its id.
    Employee fullOriginalEmployee = GetById(originalEmployee.EmployeeId);

    // Merge the data of the updated employee with the original employee.
    fullOriginalEmployee.Name = employee.Name;
    fullOriginalEmployee.Email = employee.Email;
    fullOriginalEmployee.Telephone = employee.Telephone;
    fullOriginalEmployee.BirthDate = employee.BirthDate;

    // Persist changes in database.
    SaveChanges();
}