Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 897
  • Last Modified:

VB.NET - Update Dataset

Hi, I am doing a windows application using VB.NET, I have a screen with few textboxes, all the textboxes are binded to a dataset, so once I make changes in one textbox, and when the focus moves away from the textbox, would like to update the dataset back to my database... I am using the following code:

frm.BindingContext(dataset, datatable).EndCurrentEdit()
dataadapter.Update(dataset, datatable)
dataadapter.AcceptChanges()

I place this code in the every textbox leave event, the problem is that when this code is triggered from textboxA, then the changes I made in textboxA will not be updated, but other textboxes is updated successful...  Is there any other event that I can use instead of leave event?

Thanks in advance...
0
stevenkik
Asked:
stevenkik
1 Solution
 
iboutchkineCommented:
I have tried to create a program with your condition. Here is the code that works. I have DB - TestDB and the table ITH. I have bound 2 fields to text boxes
Item to txtItem
ShopOrder to txtSO

To see the changes I also put datagrid dg on the form

Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Const DBNAME = "c:\testdb\testdb.mdb"
    Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & DBNAME & _
                          ";Persist Security Info=False"
    Private ds As New DataSet()
    Private da As New OleDbDataAdapter("select * from ITH", sConn)
    Dim cmdBuilder As New OleDbCommandBuilder(da)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'fill adapter and see data in datagrid
        da.Fill(ds, "ITH")
        dg.DataSource = ds
        dg.DataMember = "ITH"

'bind 2 fields to text boxes
        txtItem.DataBindings.Add("text", _
           ds.Tables(0), "Item")
        txtSO.DataBindings.Add("text", _
           ds.Tables(0), "ShopOrder")
    End Sub

'on leave event update datatable
    Private Sub txtItem_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtItem.Leave
'create update command using CommandBuilder
'you can creat the same command for Insert and Delete if you need
        da.UpdateCommand = cmdBuilder.GetUpdateCommand

        Me.BindingContext(ds.Tables(0)).EndCurrentEdit()
        da.Update(ds.Tables(0))
        ds.AcceptChanges()

    End Sub

'same for the second textbox
    Private Sub txtSO_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSO.Leave
        da.UpdateCommand = cmdBuilder.GetUpdateCommand

        Me.BindingContext(ds.Tables(0)).EndCurrentEdit()
        da.Update(ds.Tables(0))
        ds.AcceptChanges()

    End Sub
End Class


'Note - this will work only if the table has unique key





0
 
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by iboutchkine

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now