ASP.NET - Error:Index was out of range. Must be non-negative and less than the size of the collectio

Asked By Rohini Kuchadi on 21-Feb-13 07:22 AM
Hi 

    In my application i have binded the data to gridview from database.And now i want's update the selected row of a    gridview.I have 'Edit' button for every row in the gridview.When i click on the 'Edit' button,selected row in the gridview should be edited.And when i click on update button,edited data of a selected row in the gridview,should be updated into the database.

But my problem is,when i click on the 'Edit' button in grid am able to edit the data of a selected row in grid.But when i click on the 'Update' it is showing the following here. "Error: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index".   Kindly can i know why am getting this error.

Here is my .aspx code.
--------------------------

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
   <asp:Label ID="Label2" runat="server" Text="select State"  
                Font-Italic="False" Font-Size="Small" ></asp:Label> 
                 <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
  
            <asp:DropDownList ID="ddlstate" runat="server"  CssClass="input_field" 
         Height="28px" Width="196px">
            </asp:DropDownList>


     <asp:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="State" TargetControlID="ddlstate" LoadingText="Loading States..." PromptText="Select State"  ServiceMethod="BindStatedropdown2" ServicePath="~/DropdownWebService.asmx">
            </asp:CascadingDropDown>
         
            <asp:Button ID="btnsubmit" runat="server" Text="Submit"
                CssClass="input_field" Height="28px" Width="68px" 
        onclick="btnsubmit_Click" />
            <br />
   
                   
   
                   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                       CellPadding="4" ForeColor="#333333" GridLines="None" 
        Width="609px" onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
        onrowdeleting="GridView1_RowDeleting">
                       <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                     
                           <Columns>
                           <asp:TemplateField HeaderText="No Of Districts">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtnoofdist" Width="100%" Height="100%" runat="server" Text='<%# Eval("noofdist") %>'>
                           </asp:TextBox>
                              </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label13" runat="server" Text='<%# Eval("noofdist") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="State Capital">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtstatecapital" Width="100%" Height="100%" runat="server" Text='<%# Eval("capitalcity") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label14" runat="server" Text='<%# Eval("capitalcity") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="No Of Voters">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtnoofvoters" Width="100%" Height="100%" runat="server" Text='<%# Eval("noofvoters") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label15" runat="server" Text='<%# Eval("noofvoters") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Languages Spoken">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtlanguagesspoken" Width="100%" Height="100%" runat="server" Text='<%# Eval("languagesspoken") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label16" runat="server" Text='<%# Eval("languagesspoken") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Total Population">
                           <EditItemTemplate>
                           <asp:TextBox ID="txttotalpopulation" Width="100%" Height="100%" runat="server" Text='<%# Eval("totalpopulation") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label17" runat="server" Text='<%# Eval("totalpopulation") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Total Registered Users">
                           <EditItemTemplate>
                           <asp:TextBox ID="txttotalregisteredusers" Width="100%" Height="100%" runat="server" Text='<%# Eval("totalregisteredusers") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label18" runat="server" Text='<%# Eval("totalregisteredusers") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Chief Minister">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtchiecfminister" Width="100%" Height="100%" runat="server" Text='<%# Eval("cmname") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label19" runat="server" Text='<%# Eval("cmname") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Governor">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtgovernor" Width="100%" Height="100%" runat="server" Text='<%# Eval("governername") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label20" runat="server" Text='<%# Eval("governername") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="DGP">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtdgp" Width="100%" Height="100%" runat="server" Text='<%# Eval("dgp") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label21" runat="server" Text='<%# Eval("dgp") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="High Court Chief">
                           <EditItemTemplate>
                           <asp:TextBox ID="txthighcourtchief" Width="100%" Height="100%" runat="server" Text='<%# Eval("highcourtchiefjustice") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label22" runat="server" Text='<%# Eval("highcourtchiefjustice") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Principal Secretary">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtprincipalsecretary" Width="100%" Height="100%" runat="server" Text='<%# Eval("principalsecretary") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label23" runat="server" Text='<%# Eval("principalsecretary") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Parliament Seats">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtparliamentseats" Width="100%" Height="100%" runat="server" Text='<%# Eval("noofparliamentseats") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label24" runat="server" Text='<%# Eval("noofparliamentseats") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>


                           <asp:TemplateField HeaderText="Assembly Seats">
                           <EditItemTemplate>
                           <asp:TextBox ID="txtassemblyseats" Width="100%" Height="100%" runat="server" Text='<%# Eval("noofassembliesseats") %>'>
                         </asp:TextBox>
                          </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="Label25" runat="server" Text='<%# Eval("noofassembliesseats") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           
                           <asp:CommandField ButtonType="Button" HeaderText="Edit" ShowEditButton="True" 
                               ShowHeader="True" />
                           <asp:CommandField ButtonType="Button" HeaderText="Delete" 
                               ShowDeleteButton="True" ShowHeader="True" />                                              
                       
                      
                       </Columns>
                       <EditRowStyle BackColor="#999999" />
                       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                       <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                       <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                       <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                       <SortedAscendingCellStyle BackColor="#E9E7E2" />
                       <SortedAscendingHeaderStyle BackColor="#506C8C" />
                       <SortedDescendingCellStyle BackColor="#FFFDF8" />
                       <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                   </asp:GridView>
    
</asp:Content>

--------------------------------------------------------------------------------------------------------------------------------------

Here is my .aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;






public partial class Admin_prsnl : System.Web.UI.Page
{


    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    public string distid;


    public string stateid;
    public string constid;
    string id;
    
    protected void Page_Load(object sender, EventArgs e)
    {
      


        }




    protected void btnsubmit_Click(object sender, EventArgs e)
    {


        binddata();


    }
    private void binddata()
    {
        string id = ddlstate.SelectedValue.ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TESTConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("select noofdist,capitalcity,noofvoters,languagesspoken,totalpopulation,totalregisteredusers,cmname,governername,dgp,highcourtchiefjustice,principalsecretary,noofparliamentseats,noofassembliesseats from statemapping where stateid ='" + id + "' ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();


    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binddata();


    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        // int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        // string id = ddlstate.SelectedValue.ToString();
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string strstateid = GridView1.DataKeys[e.RowIndex].Values["stateid"].ToString();


        TextBox TextBox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnoofdist");


        TextBox TextBox2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtstatecapital");
        TextBox TextBox3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnoofvoters");
        TextBox TextBox4 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtlanguagesspoken");
        TextBox TextBox5 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttotalpopulation");
        TextBox TextBox6 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttotalregisteredusers");
        TextBox TextBox7 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtchiecfminister");
        TextBox TextBox8 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtgovernor");
        TextBox TextBox9 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdgp");
        TextBox TextBox10 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txthighcourtchief");
        TextBox TextBox11 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtprincipalsecretary");
        TextBox TextBox12 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtparliamentseats");
        TextBox TextBox13 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtassemblyseats");
        //  TextBox TextBox14 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textBox2");


        SqlCommand cmd = new SqlCommand("Update statemapping set noofdist=@noofdist,capitalcity=@capitalcity,noofvoters=@noofvoters,languagesspoken=@languagesspoken,totalpopulation=@totalpopulation,totalregisteredusers=@totalregisteredusers,cmname=@cmname,governername=@governername,dgp=@dgp,highcourtchiefjustice=@highcourtchiefjustice,principalsecretary=@principalsecretary,noofparliamentseats=@noofparliamentseats,noofassembliesseats=@noofassembliesseats from statemapping where stateid =@id ", con);


        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@noofdist", SqlDbType.VarChar, 30).Value = TextBox1.Text.Trim();
        cmd.Parameters.Add("@capitalcity", SqlDbType.VarChar, 30).Value = TextBox2.Text.Trim();
        cmd.Parameters.Add("@noofvoters", SqlDbType.VarChar, 30).Value = TextBox3.Text.Trim();
        cmd.Parameters.Add("@languagesspoken", SqlDbType.VarChar, 30).Value = TextBox4.Text.Trim();
        cmd.Parameters.Add("@totalpopulation", SqlDbType.VarChar, 30).Value = TextBox5.Text.Trim();
        cmd.Parameters.Add("@totalregisteredusers", SqlDbType.VarChar, 30).Value = TextBox6.Text.Trim();
        cmd.Parameters.Add("@cmname", SqlDbType.VarChar, 30).Value = TextBox7.Text.Trim();
        cmd.Parameters.Add("@governername", SqlDbType.VarChar, 30).Value = TextBox8.Text.Trim();
        cmd.Parameters.Add("@dgp", SqlDbType.VarChar, 30).Value = TextBox9.Text.Trim();
        cmd.Parameters.Add("@highcourtchiefjustice", SqlDbType.VarChar, 30).Value = TextBox10.Text.Trim();
        cmd.Parameters.Add("@principalsecretary", SqlDbType.VarChar, 30).Value = TextBox11.Text.Trim();
        cmd.Parameters.Add("@noofparliamentseats", SqlDbType.VarChar, 30).Value = TextBox12.Text.Trim();
        cmd.Parameters.Add("@noofassembliesseats", SqlDbType.VarChar, 30).Value = TextBox13.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        binddata();








    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ddlstate.SelectedValue.ToString();


        con.Open();
        SqlCommand cmd = new SqlCommand("delete from statemapping where stateid ='" + id + "' ", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            binddata();
        }
    


    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;






public partial class Admin_prsnl : System.Web.UI.Page
{


    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    public string distid;


    public string stateid;
    public string constid;
    string id;
    
    protected void Page_Load(object sender, EventArgs e)
    {
      


        }




    protected void btnsubmit_Click(object sender, EventArgs e)
    {


        binddata();


    }
    private void binddata()
    {
        string id = ddlstate.SelectedValue.ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TESTConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("select noofdist,capitalcity,noofvoters,languagesspoken,totalpopulation,totalregisteredusers,cmname,governername,dgp,highcourtchiefjustice,principalsecretary,noofparliamentseats,noofassembliesseats from statemapping where stateid ='" + id + "' ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();


    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binddata();


    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        // int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        // string id = ddlstate.SelectedValue.ToString();
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());  //Here am getting that error.
        string strstateid = GridView1.DataKeys[e.RowIndex].Values["stateid"].ToString();


        TextBox TextBox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnoofdist");


        TextBox TextBox2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtstatecapital");
        TextBox TextBox3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnoofvoters");
        TextBox TextBox4 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtlanguagesspoken");
        TextBox TextBox5 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttotalpopulation");
        TextBox TextBox6 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttotalregisteredusers");
        TextBox TextBox7 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtchiecfminister");
        TextBox TextBox8 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtgovernor");
        TextBox TextBox9 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdgp");
        TextBox TextBox10 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txthighcourtchief");
        TextBox TextBox11 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtprincipalsecretary");
        TextBox TextBox12 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtparliamentseats");
        TextBox TextBox13 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtassemblyseats");
        //  TextBox TextBox14 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textBox2");


        SqlCommand cmd = new SqlCommand("Update statemapping set noofdist=@noofdist,capitalcity=@capitalcity,noofvoters=@noofvoters,languagesspoken=@languagesspoken,totalpopulation=@totalpopulation,totalregisteredusers=@totalregisteredusers,cmname=@cmname,governername=@governername,dgp=@dgp,highcourtchiefjustice=@highcourtchiefjustice,principalsecretary=@principalsecretary,noofparliamentseats=@noofparliamentseats,noofassembliesseats=@noofassembliesseats from statemapping where stateid =@id ", con);


        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@noofdist", SqlDbType.VarChar, 30).Value = TextBox1.Text.Trim();
        cmd.Parameters.Add("@capitalcity", SqlDbType.VarChar, 30).Value = TextBox2.Text.Trim();
        cmd.Parameters.Add("@noofvoters", SqlDbType.VarChar, 30).Value = TextBox3.Text.Trim();
        cmd.Parameters.Add("@languagesspoken", SqlDbType.VarChar, 30).Value = TextBox4.Text.Trim();
        cmd.Parameters.Add("@totalpopulation", SqlDbType.VarChar, 30).Value = TextBox5.Text.Trim();
        cmd.Parameters.Add("@totalregisteredusers", SqlDbType.VarChar, 30).Value = TextBox6.Text.Trim();
        cmd.Parameters.Add("@cmname", SqlDbType.VarChar, 30).Value = TextBox7.Text.Trim();
        cmd.Parameters.Add("@governername", SqlDbType.VarChar, 30).Value = TextBox8.Text.Trim();
        cmd.Parameters.Add("@dgp", SqlDbType.VarChar, 30).Value = TextBox9.Text.Trim();
        cmd.Parameters.Add("@highcourtchiefjustice", SqlDbType.VarChar, 30).Value = TextBox10.Text.Trim();
        cmd.Parameters.Add("@principalsecretary", SqlDbType.VarChar, 30).Value = TextBox11.Text.Trim();
        cmd.Parameters.Add("@noofparliamentseats", SqlDbType.VarChar, 30).Value = TextBox12.Text.Trim();
        cmd.Parameters.Add("@noofassembliesseats", SqlDbType.VarChar, 30).Value = TextBox13.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        binddata();








    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ddlstate.SelectedValue.ToString();


        con.Open();
        SqlCommand cmd = new SqlCommand("delete from statemapping where stateid ='" + id + "' ", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            binddata();
        }
    


    }
}






Danasegarane Arunachalam replied to Rohini Kuchadi on 22-Feb-13 03:31 AM
I doubt there are no rows exist when the update method is called

When you click the update button the page post backs and the "binddata();" is not called , and leaves the grid empty. And you are trying to access the empty grid.

Change the Page Load event from

protected void Page_Load(object sender, EventArgs e)
   {
    
 
 
     }


to

protected void Page_Load(object sender, EventArgs e)
   {
    
 binddata();
 
     }



This should work
Rohini Kuchadi replied to Danasegarane Arunachalam on 22-Feb-13 04:18 AM
rows are present. and binddata() method is working fine.Rowcancel.RowEditing all working fine. I don't understand what is wrong in my RowUpdating code. Kindly tell me the solution how to slove that error and get the result.
Danasegarane Arunachalam replied to Rohini Kuchadi on 22-Feb-13 04:30 AM
Did you debug the code. Are there any rows in the gridview?

In the Row_Updating event type this immediate window and see the result

Gridview.Rows.Count
Tom Wilson replied to Danasegarane Arunachalam on 22-Feb-13 10:09 AM
I may have missed in your code, but you need to set the GridView's DataKeysNames property?