Click here to Skip to main content
15,888,984 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 in vb.net 2008
so far i have been able to update,delete and add new data to my project.
my problem is that every time i either update, delete or add new
the changes don't appear until i exit the program and start it all over again.
how can i be able to reflect the changes immediately
here are my codes
VB
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
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
   End Sub

   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
           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
           cmd.ExecuteNonQuery()

           con.Close()
       End Using
   End Sub


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 7-Jan-14 5:06am
v2

1 solution

The simplest approach would be to call the load method again. i.e. in your btn_save click handler just call the showitems method.

Some recommendations.

You have not got any try catches around some of the statements. Exceptions could occur and not be handled.
You have duplicate code for creating command object, connection etc. Try to think of ways to separate the logic so that you only have one function that returns the new command object thus removing duplicate code.

Hope this helps!

UPDATE

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

           'please see below
           Showitems()
       End If
   End Sub
 
Share this answer
 
v2
Comments
kef ngunjiri 8-Jan-14 6:51am    
iam new in vb how do you call the method pls explain
db7uk 8-Jan-14 6:55am    
please see my updated solution thanks.
kef ngunjiri 8-Jan-14 7:16am    
I am sorry it still has the same problem,pls help.

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