?
Solved

VB.NET - Update Dataset

Posted on 2003-03-14
3
Medium Priority
?
885 Views
Last Modified: 2012-06-27
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
Comment
Question by:stevenkik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 8141207
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9671591
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question