Delete multiple rows in gridview only deleting first record

Posted on 2011-05-06
Last Modified: 2013-11-26
I have a gridview with a delete checkbox and backend code to delete all records with a checked checkbox when a button is clicked.  The code doesn't throw an error but only deletes the first record that is checked.  Not sure what I'm doing wrong.

Protected Sub btndeleteemp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndeleteemp.Click
        ' Looping through all the rows in the GridView

        For Each row As GridViewRow In gvdeleteemp.Rows

            Dim username As String = Page.User.Identity.Name.Split("\"c)(1).ToString()

            Dim oConn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("Marketing_ResumesConnectionString").ConnectionString)
            Dim sql As New SqlCommand("spResumeProfileDeleteEmployees")
            sql.Connection = oConn
            sql.CommandType = CommandType.StoredProcedure
            Dim checkbox As CheckBox = CType(row.FindControl("cboxDeleteEmp"), CheckBox)
            Dim profile As String = Session("profile")
            Dim employee As Integer = Convert.ToInt32(gvdeleteemp.DataKeys(0).Values("EmployeeID"))

            'Check if the checkbox is checked.

            If checkbox.Checked Then

                ' Retreive parameters to delete

                sql.Parameters.Add(New SqlParameter("@username", Data.SqlDbType.VarChar, 50))
                sql.Parameters("@username").Value = username

                sql.Parameters.Add(New SqlParameter("@profile", Data.SqlDbType.VarChar, 50))
                sql.Parameters("@profile").Value = profile

                sql.Parameters.Add(New SqlParameter("@employee", Data.SqlDbType.BigInt))
                sql.Parameters("@employee").Value = employee


            End If

        Next row

    End Sub

Open in new window

Question by:imstac73
    LVL 29

    Accepted Solution

    The line below is always returning the employee from the first row :
    Dim employee As Integer = Convert.ToInt32(gvdeleteemp.DataKeys(0).Values("EmployeeID"))
    change it to :
    Dim employee As Integer = Convert.ToInt32(gvdeleteemp.DataKeys(row.RowIndex).Values("EmployeeID"))

    Author Comment

    Perfect, thank you.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now