Delete row from database using OleDb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    #1

    Delete row from database using OleDb

    I have this function working half right. The part that is working right is where I can select a row on the DataGridView, call this function using a "Delete Row" button, and then it will delete the row from the DataGridView... .However, it does not delete the row on the database.

    Can anyone help me with deleting the row from the DB using OleDb?

    Code:
    Function DeleteTableRow()
            Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
            Dim dbConnection = New OleDbConnection(TaxConnStr)
    
            Try
                Dim dbCommand As OleDbCommand = New OleDbCommand
                Dim rdr2 As OleDbDataReader
    
                Dim selectedRow = DataGridView1.SelectedRows
    
                dbCommand.CommandText = "DELETE FROM UserCriteria WHERE RowID =" & selectedRow
                If dbConnection.State = ConnectionState.Closed Then
                    dbConnection.Open()
                End If
    
                dbCommand.Connection = dbConnection
                rdr2 = dbCommand.ExecuteReader
                dbCommand.ExecuteNonQuery()
    
    
                rdr2.Close()
    
                           '''allows you to select on cell in the row to delete entire row
                For Each oneCell As DataGridViewCell In DataGridView1.SelectedCells
                    If oneCell.Selected Then
                        DataGridView1.Rows.RemoveAt(oneCell.RowIndex)
                    End If
                Next
    
    
    
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                dbConnection.Close()
            End Try
        End Function
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Your database table has a field called RowID that is populated the same way as the DataGridView?

    Comment

    • maylortaylor
      New Member
      • Nov 2012
      • 72

      #3
      Well the row in the database is called "ID" and that particular row on the DataGridView is set to hidden.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The row? You mean the field in the table in the database? If it's named ID, then you need to use ID in your SQL, not RowID.

        Comment

        • pringstonmali
          New Member
          • May 2013
          • 1

          #5
          check this datagridview tutorial



          pringston

          Comment

          Working...