如何在不加载表单的情况下显示更改? [英] How Do I Show Changes Without Loading The Form?

查看:59
本文介绍了如何在不加载表单的情况下显示更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net 2008项目结束时的iam,到目前为止,我已经能够更新,删除并向项目中添加新数据。

我的问题是,每当我更新,删除或添加新的

时,更改都不会出现,直到我退出程序并重新开始它。

how我可以立即反映更改

这里是我的代码

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

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





添加了代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

最简单的方法是再次调用load方法。即在你的btn_save点击处理程序中只需调用showitems方法。



一些建议。



你还没有我试着抓住一些陈述。可能会发生异常而不会被处理。

您有重复的代码来创建命令对象,连接等。尝试分析逻辑的方法,以便您只有一个函数返回新的命令对象因此删除重复的代码。



希望这会有所帮助!



UPDATE



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

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


这篇关于如何在不加载表单的情况下显示更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆