错误不断弹出“对象引用未设置为对象的实例 如果DataGridView1.RowCount = 0,则 MsgBox(“无法删除,表数据为空”,MsgBoxStyle.Critical,“错误消息”) 返回 如果结束
If DataGridView1.SelectedRows.Count = 0 Then
MsgBox("Cannot delete, select the table data to be deleted", MsgBoxStyle.Critical, "Error Message")
Return
End If
If MsgBox("Delete record?", MsgBoxStyle.Question + MsgBoxStyle.OkCancel, "Confirmation") = MsgBoxResult.Cancel Then Return
Try
con.Open()
Catch ex As Exception
MessageBox.Show("Connection failed !!!" & vbCrLf & "Please check that the server is ready !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Try
If AllCellsSelected(DataGridView1) = True Then
cmd.CommandType = CommandType.Text
cmd.CommandText = "DELETE FROM animal_sale"
cmd.Connection = con
cmd.ExecuteNonQuery()
End If
For Each row As DataGridViewRow In DataGridView1.SelectedRows
If row.Selected = True Then
cmd.CommandType = CommandType.Text
cmd.CommandText = "DELETE * FROM 'animal_sale' WHERE Species='" & row.DataBoundItem(1).ToString & "'"
cmd.Connection = con
cmd.ExecuteNonQuery()
End If
Next
Catch ex As Exception
MsgBox("Failed to delete" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error Message")
con.Close()
End Try