Vineet Kumar Saini

Vineet Kumar Saini

  • NA
  • 10.4k
  • 7.4m

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Dec 7 2012 12:29 AM
Hi Friends, Please help me


.cs page
========

 protected void gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {

  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString.ToString());
  int empid = Convert.ToInt32(gridview1.DataKeys[e.RowIndex].Values["emp_id"].ToString());
  SqlCommand cmd = new SqlCommand("delete from emp where emp_id=" + empid + "", con);
  con.Open();
  int temp = cmd.ExecuteNonQuery();
  con.Close();
 
 
  }

.aspx page
========

 <asp:GridView ID="gridview1" runat="server"
  onrowcancelingedit="gridview1_RowCancelingEdit"
  onrowdeleting="gridview1_RowDeleting" onrowediting="gridview1_RowEditing"
  onrowupdating="gridview1_RowUpdating"
  onselectedindexchanged="gridview1_SelectedIndexChanged">
  <Columns>
  <asp:CommandField ButtonType="Button" ShowDeleteButton="True"
  ShowEditButton="True" />
  </Columns>
 
  </asp:GridView>



when we click on delete button then i get an erro i.e. "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". So Please tell me hoe to do remove this error ?


Thanks in advance !!

Answers (9)