Index was out of range. Must be non-negative DataKeys (Asp.Net)
<p>My code is (Asp.Net , C#)</p>

<pre><code>    int index = Convert.ToInt16(e.CommandArgument);
    string str = GridView2.DataKeys[index].Value.ToString();
    Session["studyuid2"] = str;
</code></pre>

<p>The Second Line Throws me the error <strong>Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index</strong></p>

<p>and my gridview is </p>

<pre><code>&lt;asp:GridView ID="GridView2" runat="server" AllowPaging="True" Height="100px" 
                    RowStyle-Height="25px" HeaderStyle-Height="30px" FooterStyle-Height="30px" 
                    CellPadding=5 CellSpacing=5  
                    AutoGenerateColumns="False" 
                    DataSourceID="SqlDataSource1" EnableModelValidation="True" 
                    Width="100%" 
                    DataKeyNames="StudyUID" 
                    onrowcommand="GridView2_RowCommand" 
                    AllowSorting="True"&gt;
&lt;RowStyle Height="25px"&gt;&lt;/RowStyle&gt;
                    &lt;Columns&gt;
</code></pre>

<h2>                            -------------------------------</h2>

<pre><code>                    &lt;/Columns&gt;

&lt;FooterStyle Height="30px"&gt;&lt;/FooterStyle&gt;

&lt;HeaderStyle Height="30px"&gt;&lt;/HeaderStyle&gt;
                &lt;/asp:GridView&gt;
</code></pre>
