Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi there iam at the end of my project,but i have a tiny little problem here, i can be able SAVE,UPDATE and DELETE data in my database(ACCESS 2003)they work, but the results that is the UPDATED,SAVED OR DELETED data doesn't show until i close the form and load it again that's when the data that i saved or update shows or the data deleted disappears this is the last hurdle,i have to completing
I tried ds.acceptchanges() but it doesn't seem to do the trick
here are my codes

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CONTACTSTableAdapter.Fill(Me.PEOPLEDataSet.CONTACTS)
connect()

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "PEOPLE")
Showitems()
ds.AcceptChanges()
Maxrows = ds.Tables("PEOPLE").Rows.Count
inc = -1
End Sub
Private Sub Showitems()
Dim ds As New DataSet
Dim dt As New DataTable
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim da As New OleDbDataAdapter
Dim ms As New System.IO.MemoryStream
ds.Tables.Add(dt)

da = New OleDbDataAdapter("SELECT*FROM CONTACTS", con)
da.Fill(dt)


txtAgentNumber.Text = dt.Rows(0).Item(1)
txtFirstName.Text = dt.Rows(0).Item(2)
txtMiddleName.Text = dt.Rows(0).Item(3)
txtSurName.Text = dt.Rows(0).Item(4)
cboGender.Text = dt.Rows(0).Item(5)
txtAddress.Text = dt.Rows(0).Item(6)
txtPhone.Text = dt.Rows(0).Item(7)
txtEmail.Text = dt.Rows(0).Item(8)
txtNotes.Text = dt.Rows(0).Item(9)
'picPhoto.Image = dt.Rows(0).Item(10)
ds.AcceptChanges()

con.Close()

End Sub
VB
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

       If inc <> -1 Then
           Dim cmd As New OleDb.OleDbCommand(sql, con)
           Dim data As Byte() = File.ReadAllBytes(OpenFileDialog1.FileName)
           con.Open()
           sql = "INSERT INTO CONTACTS(AGENTNUMBER,FIRSTNAME,MIDDLENAME,SURNAME,GENDER,EMAIL,PHONE,ADDRESS,NOTES,PICTURE)"
           sql = sql & "VALUES(@AGENTNUMBER,@FIRSTNAME,@MIDDLENAME,@SURNAME,@GENDER,@EMAIL,@PHONE,@ADDRESS,@NOTES,@PICTURE)"
           cmd.CommandText = sql
           cmd.Connection = con
           cmd.Parameters.AddWithValue("@AGENTNUMBER", txtAgentNumber.Text)
           cmd.Parameters.AddWithValue("@FIRSTNAME", txtFirstName.Text)
           cmd.Parameters.AddWithValue("@MIDDLENAME", txtMiddleName.Text)
           cmd.Parameters.AddWithValue("@SURNAME", txtSurName.Text)
           cmd.Parameters.AddWithValue("@GENDER", cboGender.Text)
           cmd.Parameters.AddWithValue("@EMAIL", txtEmail.Text)
           cmd.Parameters.AddWithValue("@PHONE", txtPhone.Text)
           cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text)
           cmd.Parameters.AddWithValue("@NOTES", txtNotes.Text)
           cmd.Parameters.AddWithValue("@PICTURE", data)
           cmd.ExecuteNonQuery()
           cmd.Dispose()
           MsgBox("NEW RECORD ADDED")
           ds.AcceptChanges()
           btnSave.Enabled = False
           btnAdd.Enabled = True
           btnEdit.Enabled = True
           btnDelete.Enabled = True
       End If
   End Sub

   Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
       Dim cb As New OleDb.OleDbCommandBuilder(da)
       If inc <> -1 Then
           ds.Tables("PEOPLE").Rows(inc).Item(1) = txtAgentNumber.Text
           ds.Tables("PEOPLE").Rows(inc).Item(2) = txtFirstName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(3) = txtMiddleName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(4) = txtSurName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(5) = cboGender.Text
           ds.Tables("PEOPLE").Rows(inc).Item(6) = txtAddress.Text
           ds.Tables("PEOPLE").Rows(inc).Item(7) = txtPhone.Text
           ds.Tables("PEOPLE").Rows(inc).Item(8) = txtEmail.Text
           ds.Tables("PEOPLE").Rows(inc).Item(9) = txtNotes.Text
           Dim ms As New MemoryStream()
           picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
           ds.Tables("PEOPLE").Rows(inc).Item(10) = ms.ToArray()
           da.Update(ds, "PEOPLE")
           MsgBox("DATA EDITED")
           ds.AcceptChanges()
       End If
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim con As New OleDbConnection
        Dim cmd As New OleDbCommand
        Dim ms As New MemoryStream
        Dim bm As Bitmap = New Bitmap(picPhoto.Image)
        bm.Save(ms, picPhoto.Image.RawFormat)
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PEOPLE.mdb"
        con.Open()
        Using msS As MemoryStream = New MemoryStream()
            Dim bmM As Bitmap = New Bitmap(picPhoto.Image)
            bm.Save(ms, picPhoto.Image.RawFormat)
            Dim arrPic() As Byte = ms.GetBuffer()
            sql = "DELETE FROM CONTACTS WHERE AGENTNUMBER='" & txtAgentNumber.Text & "'"
            cmd.CommandText = sql
            cmd.Connection = con
            cmd.ExecuteNonQuery()
            If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
                MsgBox("Operation Cancelled")
                Exit Sub
            End If
            con.Close()
        End Using
    End Sub
 Private Sub NavigateRecords()
        Dim da As New OleDb.OleDbDataAdapter("SELECT*FROM CONTACTS", con)
        Dim dt As New DataTable
        

        da.Fill(dt)
        If ds.Tables("PEOPLE").Rows.Count > 0 Then
            txtAgentNumber.Text = ds.Tables("PEOPLE").Rows(inc).Item(1)
            txtFirstName.Text = ds.Tables("PEOPLE").Rows(inc).Item(2)
            txtMiddleName.Text = ds.Tables("PEOPLE").Rows(inc).Item(3)
            txtSurName.Text = ds.Tables("PEOPLE").Rows(inc).Item(4)
            cboGender.Text = ds.Tables("PEOPLE").Rows(inc).Item(5)
            txtAddress.Text = ds.Tables("PEOPLE").Rows(inc).Item(6)
            txtPhone.Text = ds.Tables("PEOPLE").Rows(inc).Item(7)
            txtEmail.Text = ds.Tables("PEOPLE").Rows(inc).Item(8)
            txtNotes.Text = ds.Tables("PEOPLE").Rows(inc).Item(9)
            Dim bytes As [Byte]() = ds.Tables("PEOPLE").Rows(inc).Item(10)
            Dim ms As New MemoryStream(bytes)
            picPhoto.Image = Image.FromStream(ms)
            ds.AcceptChanges()
        End If
    End Sub

    note all this code work
Posted

1 solution

as you don't use a back-end class with bindings, the simpelest way to do this:

create a sub with all your code from form1_Load.
call this sub from form1_load.
call this sub after updating, adding or deleting data.

this way you basically completely reload your data after each edit.

not the most pretty way to do this but it will work.

VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Showitems()
End Sub
Private Sub Showitems()
    Me.CONTACTSTableAdapter.Fill(Me.PEOPLEDataSet.CONTACTS)
    connect()

    da = New OleDb.OleDbDataAdapter(Sql, con)
    da.Fill(ds, "PEOPLE")
    Showitems()
    ds.AcceptChanges()
    Maxrows = ds.Tables("PEOPLE").Rows.Count
    inc = -1

    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim cmd As New OleDb.OleDbCommand(Sql, con)
    Dim da As New OleDbDataAdapter
    Dim ms As New System.IO.MemoryStream
    ds.Tables.Add(dt)

    da = New OleDbDataAdapter("SELECT*FROM CONTACTS", con)
    da.Fill(dt)


    txtAgentNumber.Text = dt.Rows(0).Item(1)
    txtFirstName.Text = dt.Rows(0).Item(2)
    txtMiddleName.Text = dt.Rows(0).Item(3)
    txtSurName.Text = dt.Rows(0).Item(4)
    cboGender.Text = dt.Rows(0).Item(5)
    txtAddress.Text = dt.Rows(0).Item(6)
    txtPhone.Text = dt.Rows(0).Item(7)
    txtEmail.Text = dt.Rows(0).Item(8)
    txtNotes.Text = dt.Rows(0).Item(9)
    'picPhoto.Image = dt.Rows(0).Item(10)
    ds.AcceptChanges()

    con.Close()

End Sub


VB
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
       Dim cb As New OleDb.OleDbCommandBuilder(da)
       If inc <> -1 Then
           ds.Tables("PEOPLE").Rows(inc).Item(1) = txtAgentNumber.Text
           ds.Tables("PEOPLE").Rows(inc).Item(2) = txtFirstName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(3) = txtMiddleName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(4) = txtSurName.Text
           ds.Tables("PEOPLE").Rows(inc).Item(5) = cboGender.Text
           ds.Tables("PEOPLE").Rows(inc).Item(6) = txtAddress.Text
           ds.Tables("PEOPLE").Rows(inc).Item(7) = txtPhone.Text
           ds.Tables("PEOPLE").Rows(inc).Item(8) = txtEmail.Text
           ds.Tables("PEOPLE").Rows(inc).Item(9) = txtNotes.Text
           Dim ms As New MemoryStream()
           picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
           ds.Tables("PEOPLE").Rows(inc).Item(10) = ms.ToArray()
           da.Update(ds, "PEOPLE")
           MsgBox("DATA EDITED")
           ds.AcceptChanges()
       End If
Showitems()
End Sub
 
Share this answer
 
v3
Comments
Member 10436009 2-Dec-13 4:26am    
ok,what do i put inside that sub?
Member 10436009 2-Dec-13 4:29am    
i am new in vb and i think i have databounded my project
w1sph 2-Dec-13 4:32am    
since your at the end of your project I won't change too much.

added 'Showitems()' after the update/insert/delete subs.
move the dataset.Fill() lines from Form1_Load to Showitem().
Member 10436009 2-Dec-13 5:05am    
iam trying but i can't navigate through my database it gives me this error 'object no set at an instance' at the navigation codes
w1sph 2-Dec-13 5:08am    
can you post the line of code where this error occurs?
also: I noticed: your record will be deleted, even if the user clicks no.

If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If

move that up at least above ExecuteNonQuery()

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