Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
When i delete the record using gridview_rowDeleting() it will be deleted two time.
when it deletes first time, it will deleted value properly.
Why it will come second time for the same record deletion. here i am getting error. Index range must be out of driven
see code

Protected Sub grdDeptEmp_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles grdDeptEmp.RowDeleting
        'Dim EmpNo As Integer = Convert.ToInt32(grdDeptEmp.DataKeys(e.RowIndex).Values(0).ToString())
        Dim EmpNo As Integer = CInt(grdDeptEmp.DataKeys(e.RowIndex).Value)
        'Dim lblEmpNo As Label = DirectCast(grdDeptEmp.Rows(e.RowIndex).FindControl("lblEmpNo"), Label)
        If EmpNo.ToString IsNot Nothing Then
            Conn.Open()
            StrQry = ""
            StrQry = "delete from OtherDeptEmp where emp_number='" & EmpNo & "'"
            Cmd.Connection = Conn
            Cmd.CommandText = StrQry
            'Cmd.ExecuteNonQuery()
            Conn.Close()
        End If
        FillGrid()
    End Sub

what is the problem over here..?
Posted
Updated 11-Jul-11 22:49pm
v2
Comments
gani7787 12-Jul-11 5:08am    
I will fire two times. in the second time the EmpNo is field getting error. because i deleted the record. again it will read the same record when coming inside.
gani7787 12-Jul-11 5:18am    
the Ecact Error is "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

call the<b> databind() </b>function in the fillgrid() method after every successfull operation
 
Share this answer
 
v2
Hi,

I think you should handle this in RowCommand event.
You may try this:
 Protected Sub grdDeptEmp_RowCommand(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdDeptEmp.RowCommand
   If e.CommandName = "DeleteCmd" Then
      Dim rowNo As Integer = Integer.Parse(e.CommandArgument)
      Dim EmpNo As String = grdResult.DataKeys(rowNo).Item("emp_number").ToString.Trim
      Conn.Open()
      StrQry = ""
      StrQry = "delete from OtherDeptEmp where emp_number='" & EmpNo & "'"
      Cmd.Connection = Conn
      Cmd.CommandText = StrQry
      'Cmd.ExecuteNonQuery()
      Conn.Close()
   End If
   FillGrid()
End Sub



In your client code Grid example:
<asp:ButtonField CommandName="DeleteCmd" Text="Delete"/>

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900