我想知道我的代码有什么问题。如果我想删除,我必须勾选复选框,它允许我删除一条记录,但是当我想删除多条记录时,它不会给我一个错误,但记录没有被删除。这是我的代码
Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chkTest As CheckBox = DirectCast(sender, CheckBox)
Dim grdRow As GridViewRow = DirectCast(chkTest.NamingContainer, GridViewRow)
Dim bin As TextBox = DirectCast(grdRow.FindControl("txtBin"), TextBox)
Dim wheatGrist As TextBox = DirectCast(grdRow.FindControl("txtWheatGrist"), TextBox)
Dim HB43S As TextBox = DirectCast(grdRow.FindControl("txtHB43S"), TextBox)
Dim M_NIR As TextBox = DirectCast(grdRow.FindControl("txtNIR"), TextBox)
Dim HB43 As TextBox = DirectCast(grdRow.FindControl("txtHB43"), TextBox)
Dim WG_NIR As TextBox = DirectCast(grdRow.FindControl("txtWGNIR"), TextBox)
Dim glutematic As TextBox = DirectCast(grdRow.FindControl("txtGlutematic"), TextBox)
Dim handwash As TextBox = DirectCast(grdRow.FindControl("txtHandwash"), TextBox)
Dim distilledWater As TextBox = DirectCast(grdRow.FindControl("txtDistilled"), TextBox)
If chkTest.Checked Then
bin.[ReadOnly] = False
wheatGrist.[ReadOnly] = False
HB43S.[ReadOnly] = False
M_NIR.[ReadOnly] = False
HB43.[ReadOnly] = False
WG_NIR.[ReadOnly] = False
glutematic.[ReadOnly] = False
handwash.[ReadOnly] = False
distilledWater.[ReadOnly] = False
bin.ForeColor = System.Drawing.Color.Blue
wheatGrist.ForeColor = System.Drawing.Color.Blue
HB43S.ForeColor = System.Drawing.Color.Blue
M_NIR.ForeColor = System.Drawing.Color.Blue
HB43.ForeColor = System.Drawing.Color.Blue
WG_NIR.ForeColor = System.Drawing.Color.Blue
glutematic.ForeColor = System.Drawing.Color.Blue
handwash.ForeColor = System.Drawing.Color.Blue
distilledWater.ForeColor = System.Drawing.Color.Blue
Else
bin.[ReadOnly] = True
wheatGrist.[ReadOnly] = True
HB43S.[ReadOnly] = True
M_NIR.[ReadOnly] = True
HB43.[ReadOnly] = True
WG_NIR.[ReadOnly] = True
glutematic.[ReadOnly] = True
handwash.[ReadOnly] = True
distilledWater.[ReadOnly] = True
bin.ForeColor = System.Drawing.Color.Black
wheatGrist.ForeColor = System.Drawing.Color.Black
HB43S.ForeColor = System.Drawing.Color.Black
M_NIR.ForeColor = System.Drawing.Color.Black
HB43.ForeColor = System.Drawing.Color.Black
WG_NIR.ForeColor = System.Drawing.Color.Black
glutematic.ForeColor = System.Drawing.Color.Black
handwash.ForeColor = System.Drawing.Color.Black
distilledWater.ForeColor = System.Drawing.Color.Black
End If
End Sub
Private Sub UncheckAll()
For Each row As GridViewRow In GridView1.Rows
Dim chkUncheck As CheckBox = DirectCast(row.FindControl("chkSelect"), CheckBox)
Dim bin As TextBox = DirectCast(row.FindControl("txtBin"), TextBox)
Dim wheatGrist As TextBox = DirectCast(row.FindControl("txtWheatGrist"), TextBox)
Dim HB43S As TextBox = DirectCast(row.FindControl("txtHB43S"), TextBox)
Dim M_NIR As TextBox = DirectCast(row.FindControl("txtNIR"), TextBox)
Dim HB43 As TextBox = DirectCast(row.FindControl("txtHB43"), TextBox)
Dim WG_NIR As TextBox = DirectCast(row.FindControl("txtWGNIR"), TextBox)
Dim glutematic As TextBox = DirectCast(row.FindControl("txtGlutematic"), TextBox)
Dim handwash As TextBox = DirectCast(row.FindControl("txtHandwash"), TextBox)
Dim distilledWater As TextBox = DirectCast(row.FindControl("txtDistilled"), TextBox)
chkUncheck.Checked = False
bin.ForeColor = System.Drawing.Color.Black
wheatGrist.ForeColor = System.Drawing.Color.Black
HB43S.ForeColor = System.Drawing.Color.Black
M_NIR.ForeColor = System.Drawing.Color.Black
HB43.ForeColor = System.Drawing.Color.Black
WG_NIR.ForeColor = System.Drawing.Color.Black
glutematic.ForeColor = System.Drawing.Color.Black
handwash.ForeColor = System.Drawing.Color.Black
distilledWater.ForeColor = System.Drawing.Color.Black
Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create String Collection to store IDs of
'records to be deleted
Dim idCollection As New StringCollection()
Dim strID As Integer
'Loop through GridView rows to find checked rows
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim chkDelete As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
If chkDelete IsNot Nothing Then
If chkDelete.Checked Then
strID = GridView1.Rows(i).Cells(1).Text
idCollection.Add(strID)
End If
End If
Next
If idCollection.Count > 0 Then
'Call the method to Delete records
DeleteRecords(idCollection)
' rebind the GridView
GridView1.DataBind()
'Else
'lblMessage.Text = "Please select any row to delete"
End If
End Sub
Private Sub DeleteRecords(ByVal idCollection As StringCollection)
'Create sql Connection and Sql Command
Dim cnnOLEDB As New OleDbConnection(strConnectionString)
Dim cmd As New OleDbCommand
Dim IDs As Integer
For Each id As Integer In idCollection
IDs += id & ","
Next
Try
Dim test As Integer = IDs
Dim sql As String = "DELETE FROM [1BK] WHERE [sampleID] in (" & test & ")"
Dim ANS As Integer
ANS = MsgBox("Are you sure want to delete selected record ?" & vbCrLf & vbCrLf, MsgBoxStyle.YesNo)
If ANS = vbYes Then
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.Connection = cnnOLEDB
cnnOLEDB.Open()
cmd.ExecuteNonQuery()
End If
ANS = 0
Catch ex As Exception
Dim errorMsg As String = "Error in Deletion"
errorMsg += ex.Message
Throw New Exception(errorMsg)
Finally
cnnOLEDB.Close()
End Try
End Sub