Click here to Skip to main content
15,891,855 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Imports System.Data.OleDb
Public Class FormAttacheeInfo
    Dim con As New OleDbConnection

   Private Sub FormAttacheeInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.AllowUserToAddRows = False
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\TONYMA\Documents\RegistrationDB.accdb"
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
        da.Fill(dt)
        DataGridView1.DataSource = dt.DefaultView
        con.Close()
    End Sub


    Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        If DataGridView1.SelectedCells.Count > 0 Then
            If MessageBox.Show("You want to Makes changes to this Record ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                Dim dt As New DataTable
                Dim ds As New DataSet
                ds.Tables.Add(dt)
                Dim da As New OleDbDataAdapter
                con.Open()
                da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
                da.Fill(dt)

                Dim i As Integer = DataGridView1.SelectedCells(0).IsInEditMode
                dt.Rows(i).BeginEdit()
                'which code am i missing here so as to update changes into database when cell content has been changed

               
                dt.Rows(i).EndEdit()

                'code for refreshing datagridview1 that user can see the updated cell content

                con.Close()
                MessageBox.Show("Attachee's record successfully updated", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Me.Focus()
            End If
        End If
          
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If DataGridView1.SelectedRows.Count > 0 Then
            If MessageBox.Show("You want to Clear this Record ?", "ATTACHEES RECORD", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                Dim dt As New DataTable
                Dim ds As New DataSet
                ds.Tables.Add(dt)
                Dim da As New OleDbDataAdapter
                con.Open()
                da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
                da.Fill(dt)
                Dim i As Integer = DataGridView1.SelectedRows(0).Index
                dt.Rows(i).BeginEdit()
                DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
                dt.Rows(i).Delete()
                dt.Rows(i).EndEdit()
                Dim cmd As New OleDbCommandBuilder(da)
                ' Can't understand this exception , guys please help with a delete code ?
                da.Update(dt)

                DataGridView1.DataSource = dt.DefaultView
                con.Close()
                MessageBox.Show("Attachee deleted", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Me.Focus()
            End If
        End If
    End Sub
End Class


What I have tried:

i have tried even google . NO ANSWER SO FAR
Posted
Comments
Richard MacCutchan 14-Jan-20 13:56pm    
"Can't understand this exception"
Then please add the exact text of the exception so we can see it.

MessageBox.Show("Attachee's record successfully updated"
Don't post success messages until you have actually checked that your code was successful.
Dave Kreskowiak 14-Jan-20 14:00pm    
This code has nothing to do with Excel at all. It uses an Access database though.

You're going to have to supply a more detailed explanation of what you're doing, what you're using, and the exception message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900