title: Webforms DetailsView not saving data on edit/update
body: <p>The user comes to this page to edit/update details for particular Employee. The employee details are shown in a DetailsView, which is bound to Northwind database. The problem is when I click Edit and change some data and then click Update - the fields returns to the initial state as if nothing has changed.</p>
  
  <p>Webform code:</p>
  
  <pre><code>  &lt;form id="form1" runat="server"&gt;
  
      &lt;asp:DetailsView ID="DetailsViewEditEmployee" 
          runat="server" 
          AutoGenerateRows="False"         
          Height="50px" 
          Width="314px"            
          OnItemCommand="DetailsViewEditEmployee_ItemCommand"
          OnModeChanging="DetailsViewEditEmployee_ModeChanging"
          OnItemUpdating="DetailsViewEditEmployee_ItemUpdating"
          OnItemUpdated="DetailsViewEditEmployee_ItemUpdated" &gt;
          &lt;Fields&gt;
              &lt;asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="False" SortExpression="EmployeeID" /&gt;
              &lt;asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /&gt;
              &lt;asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /&gt;
              &lt;asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /&gt;
              &lt;asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /&gt;
              &lt;asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /&gt;               
              &lt;asp:CommandField ButtonType="Button" CausesValidation="False" ShowEditButton="True" /&gt;           
          &lt;/Fields&gt;
      &lt;/asp:DetailsView&gt;
      CustomerID:
     &lt;asp:Literal Text="text" runat="server" id="LiteralCustomerId" Mode="Encode" /&gt;
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:NorthwindConnectionString %&gt;" DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Title], [Address], [City]) VALUES (@LastName, @FirstName, @Title, @Address, @City)" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [Address], [City] FROM [Employees]" UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title, [Address] = @Address, [City] = @City WHERE [EmployeeID] = @EmployeeID"&gt;
          &lt;UpdateParameters&gt;
              &lt;asp:Parameter Name="LastName" Type="String" /&gt;
              &lt;asp:Parameter Name="FirstName" Type="String" /&gt;
              &lt;asp:Parameter Name="Title" Type="String" /&gt;
              &lt;asp:Parameter Name="Address" Type="String" /&gt;
              &lt;asp:Parameter Name="City" Type="String" /&gt;
              &lt;asp:Parameter Name="EmployeeID" Type="Int32" /&gt;
          &lt;/UpdateParameters&gt;
      &lt;/asp:SqlDataSource&gt;        
  &lt;/form&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>public partial class EmployeesEdit : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Request.Params["Id"] == null)
          {
              Response.Redirect("EmployeesRepeater.aspx");
          }
  
          this.LiteralCustomerId.Text = Request.Params["id"];
          var id = int.Parse(Request.Params["id"]);
          using (var context = new NorthwindEntities())
          {
              var employees = context.Employees;
              var empToList = employees.ToList();
              var current =
                  from emp in empToList
                  where emp.EmployeeID == id
                  select emp;
              this.DetailsViewEditEmployee.DataSource = current;
              this.DetailsViewEditEmployee.DataBind();
          }
      }
  
      protected void DetailsViewEditEmployee_ModeChanging(object sender, DetailsViewModeEventArgs e)
      {
          if (e.NewMode == DetailsViewMode.Edit)
          {
              this.DetailsViewEditEmployee.ChangeMode(e.NewMode);
              this.DetailsViewEditEmployee.DataBind(); // add this and check
          }
          if (e.CancelingEdit)
          {
              this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.ReadOnly);
              this.DetailsViewEditEmployee.DataBind(); // add this and check
          }
      }
  
      protected void DetailsViewEditEmployee_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
      {
          for (int i = 0; i &lt; e.NewValues.Count; i++)
          {
              if (e.NewValues[i] != null)
              {
                  e.NewValues[i] = Server.HtmlEncode(e.NewValues[i].ToString());
              }
          }
      }
  
      protected void DetailsViewEditEmployee_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
      {
          this.DetailsViewEditEmployee.DataBind();          
      }
  
      protected void DetailsViewEditEmployee_ItemCommand(object sender, DetailsViewCommandEventArgs e)
      {
          if (e.CommandName.Equals("New"))
          {
              this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.Insert);
              this.DetailsViewEditEmployee.DataBind();
          }
          else if (e.CommandName.Equals("Edit"))
          {
              this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.Edit);
              this.DetailsViewEditEmployee.DataBind();
          }
      }        
  }
  </code></pre>