hi everyone. i am having issues with the following code below. it only deletes 1 record at a time even if multiple records are selected. thanks in advance for the help.
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
MsgBox("Are you sure you want to delete the following records?", MsgBoxStyle.YesNo, "UTM Online Library")
If vbYes Then
For Each row As GridViewRow In gvBookList.Rows
Dim checkbox As CheckBox = CType(row.FindControl("chkBox"), CheckBox)
'Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value)
Dim checkedCount As Integer = 0
If checkbox.Checked Then
'checkedCount = checkedCount + 1
Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value)
Dim myConnection As New SqlConnection(myConStr)
Dim mySqlCommand As New SqlCommand()
Dim sqlStr As String
sqlStr = "DELETE from tblBooks WHERE fldBookID = @fldBookID"
mySqlCommand.Parameters.AddWithValue("@fldBookID", rowID)
mySqlCommand.Connection = myConnection
mySqlCommand.CommandText = sqlStr
myConnection.Open()
mySqlCommand.ExecuteNonQuery()
myConnection.Close()
End If
Response.Redirect("Admin.aspx")
'gvBookList.DataBind()
Next
End If
End Sub
you are doing response.redirect inside for each loop...
hence, after first update command, it will immediately redirect u to other page and will not get a chance to complete for loop...
move response.redirect after for each loop like this
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
MsgBox("Are you sure you want to delete the following records?", MsgBoxStyle.YesNo, "UTM Online Library")
If vbYes Then
For Each row As GridViewRow In gvBookList.Rows
Dim checkbox As CheckBox = CType(row.FindControl("chkBox"), CheckBox)
'Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value)
Dim checkedCount As Integer = 0
If checkbox.Checked Then
'checkedCount = checkedCount + 1
Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value)
Dim myConnection As New SqlConnection(myConStr)
Dim mySqlCommand As New SqlCommand()
Dim sqlStr As String
sqlStr = "DELETE from tblBooks WHERE fldBookID = @fldBookID"
mySqlCommand.Parameters.AddWithValue("@fldBookID", rowID)
mySqlCommand.Connection = myConnection
mySqlCommand.CommandText = sqlStr
myConnection.Open()
mySqlCommand.ExecuteNonQuery()
myConnection.Close()
End If
'gvBookList.DataBind()
Next
Response.Redirect("Admin.aspx")
End If
End Sub
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
Marked as answer by darksyfer on Apr 30, 2012 04:40 PM
darksyfer
Member
17 Points
37 Posts
deleting multiple checked rows in gridview
Apr 30, 2012 04:32 PM|LINK
hi everyone. i am having issues with the following code below. it only deletes 1 record at a time even if multiple records are selected. thanks in advance for the help.
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click MsgBox("Are you sure you want to delete the following records?", MsgBoxStyle.YesNo, "UTM Online Library") If vbYes Then For Each row As GridViewRow In gvBookList.Rows Dim checkbox As CheckBox = CType(row.FindControl("chkBox"), CheckBox) 'Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value) Dim checkedCount As Integer = 0 If checkbox.Checked Then 'checkedCount = checkedCount + 1 Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value) Dim myConnection As New SqlConnection(myConStr) Dim mySqlCommand As New SqlCommand() Dim sqlStr As String sqlStr = "DELETE from tblBooks WHERE fldBookID = @fldBookID" mySqlCommand.Parameters.AddWithValue("@fldBookID", rowID) mySqlCommand.Connection = myConnection mySqlCommand.CommandText = sqlStr myConnection.Open() mySqlCommand.ExecuteNonQuery() myConnection.Close() End If Response.Redirect("Admin.aspx") 'gvBookList.DataBind() Next End If End Subkedarrkulkar...
All-Star
34505 Points
5554 Posts
Re: deleting multiple checked rows in gridview
Apr 30, 2012 04:38 PM|LINK
you are doing response.redirect inside for each loop...
hence, after first update command, it will immediately redirect u to other page and will not get a chance to complete for loop...
move response.redirect after for each loop like this
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click MsgBox("Are you sure you want to delete the following records?", MsgBoxStyle.YesNo, "UTM Online Library") If vbYes Then For Each row As GridViewRow In gvBookList.Rows Dim checkbox As CheckBox = CType(row.FindControl("chkBox"), CheckBox) 'Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value) Dim checkedCount As Integer = 0 If checkbox.Checked Then 'checkedCount = checkedCount + 1 Dim rowID As Integer = Convert.ToInt32(gvBookList.DataKeys(row.RowIndex).Value) Dim myConnection As New SqlConnection(myConStr) Dim mySqlCommand As New SqlCommand() Dim sqlStr As String sqlStr = "DELETE from tblBooks WHERE fldBookID = @fldBookID" mySqlCommand.Parameters.AddWithValue("@fldBookID", rowID) mySqlCommand.Connection = myConnection mySqlCommand.CommandText = sqlStr myConnection.Open() mySqlCommand.ExecuteNonQuery() myConnection.Close() End If 'gvBookList.DataBind() Next Response.Redirect("Admin.aspx") End If End Subhope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
darksyfer
Member
17 Points
37 Posts
Re: deleting multiple checked rows in gridview
Apr 30, 2012 04:40 PM|LINK
thank you