Question Database book sample code not saving changes to the database

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

I'm using the book titled "Database Programming with Visual Basic® .NET and ADO.NET: Tips, Tutorials, and Code".

I'm going through the chapter that has me do data binding by setting the properties of a DataGrid and TextBoxes.

It had me drag a DataAdapter onto the form using the wizard and had me generate a DataSet. This part works fine. It also had me set the binding through the Text propert of each TextBox. This also works fine as it displays the correct data as the user navigates through the DataGrid.

I think there may be something missing from the code samples in the book because saving the data back to the database is not working and even no errors are reported.

Here is the code from the book that writes changed text in the TextBoxes back to SQL Server. Can you tell me what the book is missing?

VB.NET:
    Private Sub SaveRecord()

        ' Use the BindingContext class to end current editing so that we can update the server.
        '--------------------------------------------------------------------------------------
        BindingContext(DataSetCustomerIndividual, "Customers").EndCurrentEdit()

        ' Perform the requested task at the dataset level using the data adapter.
        '------------------------------------------------------------------------
        SqlDataAdapterCustomerIndividual.Update(DataSetCustomerIndividual, "Customers")

        ' By accepting the changes, the data gets sent back to the server.
        '-----------------------------------------------------------------
        DataSetCustomerIndividual.AcceptChanges()

    End Sub

Thanks.

Truly,
Emad
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,743
Location
Sydney, Australia
Programming Experience
10+
That code looks OK, but the comments are dodgy.

First, do you have a BindingSource? If so, forget the BindingContext and just call End Edit on the BindingSource. I'm guessing that your book dates back to before the BindingSource class existed.

Second, I have no idea what this is supposed to mean:
Perform the requested task at the dataset level using the data adapter.
The Update method saves the changes from the DataTable to the database.

Finally, this:
By accepting the changes, the data gets sent back to the server.
is plain wrong. AcceptChanges has no effect on the database at all. What it does is clear all the changed flags from the DataSet, which means that Update would find no changes to save. That's pointless though, because Update does that implicitly by default after saving.

Most likely you are saving changes, but you are just looking in the wrong place or at the wrong time. The first thing to do is to check the value returned by Update. If it's zero then the DataTable contains no changes to save. If it's not zero then there were changes and they were saved.
 

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi,

Thanks for the reply.

About the book, it's an old ebook from 2002.

Yes, there is a BindingSource it was created at design time when I set the TextBox binding in the Properties tab under the (DataBindings) (Advanced) Text property. That part works fine because it displays data in the TextBoxes.

Those dodgy comments are pasted directly from the book so I will correct them. ;)

It now works after making the changes you suggested.

Here's the code now:

VB.NET:
    Private Sub SaveRecord()

        Dim intTotalRows As Integer

        ' Apply changes to the data source.
        '----------------------------------
        CustomerIndividualBindingSource.EndEdit()

        ' Save the changes from the DataTable to the database.
        '-----------------------------------------------------
        intTotalRows = SqlDataAdapterCustomerIndividual.Update(DataSetCustomerIndividual, "Customers")

    End Sub

Thanks so much for your help.

Truly,
Emad
 
Top Bottom