title: Please what's causing this "Index was out of range Must be non-negative... error"?
body: <p>Please can someone really tell me what's wrong with this code that I'm having this error:</p>
  
  <pre><code>System.ArgumentOutOfRangeException was caught
  Message=Index was out of range. Must be non-negative and less than the size of the collection.
  Parameter name: index
  
  int index = Int32.Parse(e.CommandArgument.ToString());
  long Id = long.Parse(gvGrid.DataKeys[index][0].ToString());
  var stuobj = ctx.stockUnits.SingleOrDefault(st =&gt; st.ID == Id);
  
                          if (stuobj != null)
                              this.txtStockUnit.Text = stuobj.NAME;
  
                          ViewState["Id"] = Id; 
  </code></pre>
  
  <p>In the mark up (Designer source)    this is what I have:</p>
  
  <pre><code>&lt;asp:GridView ID="gvGrid" runat="server" 
              OnPageIndexChanging = "gvGrid_PageIndexChanging" AutoGenerateColumns="False" 
              BackColor="White" CssClass = "mGrid"  DataKeyNames ="Id,NAME"
                  OnRowCommand="gvGrid_RowCommand"
  
  ...&lt;Columns&gt;
  
          &lt;asp:BoundField DataField="NAME" HeaderText="STOCK UNIT NAME" HeaderStyle-CssClass="Headerhodder"
                          FooterStyle-CssClass="Headerhodder" ItemStyle-HorizontalAlign="Left"  HeaderStyle-HorizontalAlign="Left" 
                          ItemStyle-VerticalAlign="Middle" /&gt;
             &lt;asp:TemplateField HeaderStyle-CssClass="Headerhodder" FooterStyle-CssClass="Headerhodder"
                          ItemStyle-HorizontalAlign="Center"  ItemStyle-Width="50px"
              ItemStyle-VerticalAlign="Middle"&gt;
              &lt;ItemTemplate&gt;
  
                              &lt;asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/img/pencil.png" CommandName="EditStockUnit" CommandArgument ='&lt;%# Container.DataItemIndex %&gt; '
                              AlternateText="Edit Stock Unit" ToolTip="Edit Stock unit record"  CausesValidation="false" /&gt;
  </code></pre>
  
  <p>Is there anything missing here? Thanks for the assistance.</p>