title: DetailsView: Index was out of range
body: <p>I'm trying to get the datakey value from <code>DetailsView</code> and paste it in a form. I've included all the datakeynames but still couldn't get the value to paste it in my formview but I've encountered this probles: </p>
  
  <blockquote>
    <p>Index was out of range. Must be
    non-negative and less than the size of
    the collection. Parameter name: index</p>
  </blockquote>
  
  <p>I've tried <code>SelectedValue</code> and SelectedRow but it only retrieve the primary keys.</p>
  
  <p>Attached here is my code-behind. Help would be much appreciated! </p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>protected void FormView1_DataBound(object sender, EventArgs e)
  {
      if(FormView1.CurrentMode == FormViewMode.Insert)
      {
          TextBox bookid = FormView1.FindControl("bookidTextBox") as TextBox;
          bookid.Text = DetailsView1.DataKey[1].ToString();
  
          TextBox employee = FormView1.FindControl("EmployeeID") as TextBox;
          employee.Text = DetailsView1.DataKey[2].ToString();
      }
  }
  </code></pre>
  
  <p>Heres the code from the control</p>
  
  <pre><code>&lt;asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
              DataKeyNames="reservationid,bookid,EmployeeID,reservedate" DataSourceID="booklendingDataSource" Height="50px" 
              Width="300px" &gt;
              &lt;Fields&gt;
  
                  &lt;asp:CommandField ButtonType="Button" ShowSelectButton="true" SelectText="Lend" /&gt;
                  &lt;asp:BoundField DataField="reservationid, bookid, EmployeeID" HeaderText="reservationid" 
                      InsertVisible="False" ReadOnly="True" SortExpression="reservationid" 
                      Visible="false" /&gt;
  
                  &lt;asp:BoundField DataField="bookid" HeaderText="bookid" 
                      Visible="false" SortExpression="bookid" /&gt;
  
                  &lt;asp:BoundField DataField="booktitle" HeaderText="Title" 
                      SortExpression="booktitle" /&gt;
  
                  &lt;asp:BoundField DataField="EmployeeID" HeaderText="Emp PIN" 
                      SortExpression="EmployeeID" /&gt;
  
                  &lt;asp:BoundField DataField="reservedate" HeaderText="Reserve date" 
                      SortExpression="reservedate" /&gt;
  
                  &lt;asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted" 
                      SortExpression="isdeleted" /&gt;
  
              &lt;/Fields&gt;
          &lt;/asp:DetailsView&gt;
  ...
  
  &lt;asp:FormView ID="FormView1" runat="server" DataKeyNames="lenid" 
              DataSourceID="lendformDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound" &gt;
              &lt;EditItemTemplate&gt;
  
          ...
  
              &lt;/EditItemTemplate&gt;
              &lt;InsertItemTemplate&gt;
                  bookid:
                  &lt;asp:TextBox ID="bookidTextBox" runat="server" 
                      Text='&lt;%# Bind("bookid") %&gt;' /&gt;
                  &lt;br /&gt;
                  EmployeeID:
                  &lt;asp:TextBox ID="EmployeeIDTextBox" runat="server" 
                      Text='&lt;%# Bind("EmployeeID") %&gt;' /&gt;
                  &lt;br /&gt;
                  department:
                  &lt;asp:TextBox ID="departmentTextBox" runat="server" 
                      Text='&lt;%# Bind("department") %&gt;' /&gt;
                  &lt;br /&gt;
                  dateborrowed:
                  &lt;asp:TextBox ID="dateborrowedTextBox" runat="server" 
                      Text='&lt;%# Bind("dateborrowed") %&gt;' /&gt;
                  &lt;br /&gt;
                  expdateofreturn:
                  &lt;asp:TextBox ID="expdateofreturnTextBox" runat="server" 
                      Text='&lt;%# Bind("expdateofreturn") %&gt;' /&gt;
                  &lt;br /&gt;
  
                  &lt;asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                      CommandName="Insert" Text="Insert" /&gt;
                  &amp;nbsp;&lt;asp:LinkButton ID="InsertCancelButton" runat="server" 
                      CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
              &lt;/InsertItemTemplate&gt;
              &lt;ItemTemplate&gt;
  </code></pre>