-1

I have a datagridview with data, one column (EmployeeID) has the same value because it can occur more than once, I have a delete button which when clicked it deletes all the rows which has the EmployeeID 1 lets say. Is it possible to make it work so that it only deletes the row that I have selected other than it deleting all rows with a particular value.

I have the code here:

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        'check for the selected item in the list
        If Me.DataGridView1.Rows.Count > 0 Then
            If Me.DataGridView1.SelectedRows.Count > 0 Then
                Dim intemployeeno As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmployeeID").Value
                'open connection if not already opened
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                'delete data
                Dim cmd As New OleDb.OleDbCommand
                cmd.Connection = cnn
                'cmd.CommandText = "DELETE * FROM attendance " & intemployeeno
                cmd.CommandText = "DELETE FROM attendance WHERE employeeID=" & intemployeeno
                cmd.ExecuteNonQuery()
                'refresh data
                Me.refreshdata()
                'Display pop up alert before deleting row of data
            Else
                MessageBox.Show("Select 1 row before you hit Delete")
                'close connection
                cnn.Close()

            End If
        End If
    End Sub

Thanks

Next part of code

Public Class Form4

Dim cnn As New OleDb.OleDbConnection

'open connection to database

Private Sub refreshdata()
    If Not cnn.State = ConnectionState.Open Then
        'open connection
        cnn.Open()
    End If
    'create new data and insert the data values
    Dim da As New OleDb.OleDbDataAdapter("SELECT TestNumber as [TestNumber], EmployeeID as [EmployeeID], " & _
                                    "firstname as [FirstName], lastname as [LastName], date as [Date], holiday as [Holiday], halfday as [HalfDay], other as [Other], sick as [Sick], unpaidholiday as [UnpaidHoliday], daystaken as [DaysTaken], datefrom as [DateFrom], dateto as [DateTo] " & _
                                    "FROM Attendance ORDER BY FirstName", cnn)
    Dim dt As New DataTable
    'fill data into datatable
    da.Fill(dt)
    'offer data to be placed in datagridview
    Me.DataGridView1.DataSource = dt
    'close connection
    cnn.Close()
End Sub
7
  • I'd hope so. Update your WHERE to add additional parameters so that you only delete 1 row as to how... without knowing your data I couldn't say.
    – Daniel
    Jul 1, 2014 at 14:52
  • can i just delete the highlighted row without indicating any specific type of value before it deletes the row? Jul 1, 2014 at 14:59
  • What is the primary key of the attendance table? Jul 1, 2014 at 15:08
  • I have not set one for the attendance table Jul 1, 2014 at 15:10
  • Then how can you possibly expect to be able to single out a particular row? Jul 1, 2014 at 15:10

2 Answers 2

0

Your datagridview needs a column with the attendance table's primary key. It can be a hidden (non-visible) column if you don't want the user to see it.

Without a primary key (or a unique constraint, but let's use the PK for simplicity's sake), there is no way for the database to ensure that you are referring to a single, specific row.

Then, you need to change the following lines in your VB code:

Dim myPrimaryKey As Integer = Me.DataGridView1.SelectedRows(0).Cells("PrimaryKeyColumnName").Value

Change your delete statement as well:

cmd.CommandText = "DELETE FROM attendance WHERE AttendancePK=" & myPrimaryKey
5
  • OK great, i got it to work now but how do i remove the column with the primary key that i don't want the user to see? Jul 1, 2014 at 15:48
  • Set its Visible property to false. Jul 1, 2014 at 15:50
  • i used sql to enter in the table into datagrid. I just uploaded the code Jul 1, 2014 at 15:53
  • Going from memory here, but try Me.DataGridView1.Columns("PrimaryKeyColumnName").Visible = False. Or you can just set it in the designer, assuming that the columns aren't auto-generated. Jul 1, 2014 at 15:54
  • i can't seem to find and change it to Me.DataGridView1.Columns("PrimaryKeyColumnName").Visible = False.. Jul 1, 2014 at 16:04
0

I just did the edits in place so you'll need more changes. If the case is your user has selected one row to delete.

The changes are marked with *

Also, you test to see if cnn is connected on the way in. If you want to return with cnn still connected you'll have to set a flag to determine the state on exit.

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
    'check for the selected item in the list
    If Me.DataGridView1.Rows.Count > 0 Then
        If Me.DataGridView1.SelectedRows.Count =  1 Then   *****
            Dim intemployeeno As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmployeeID").Value
            'open connection if not already opened
            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If
            'delete data
            Dim cmd As New OleDb.OleDbCommand
            cmd.Connection = cnn
            'cmd.CommandText = "DELETE * FROM attendance " & intemployeeno
            cmd.CommandText = "DELETE FROM attendance WHERE employeeID=" & intemployeeno
            cmd.ExecuteNonQuery()
            cnn.Close()  *****  
            'refresh data
            Me.refreshdata()
            'Display pop up alert before deleting row of data
        Else
            MessageBox.Show("Select 1 row before you hit Delete")
            'close connection
          '  cnn.Close()  *****

        End If
    End If
End Sub
2
  • Douglas is closer to the OP. Although I would guess you could get the ID# from the first selected row and execute the DELETE just once, then refresh the dataset. On the original SQL looks a bit scary - a mass delete from Attendance of all records with the given ID#. Is that what you want?
    – rheitzman
    Jul 1, 2014 at 15:30
  • The problem isn't that multiple rows are selected; the problem is that SQL is deleting ALL of the rows with that particular employeeID. It isn't unique. Jul 1, 2014 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.