Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Updating a Data Source in VB.NET

I am trying to update a DataSource using the object

// SQLDataAdapter.Update(DataSet) //

The scenario is as follows:
1. I am filling a DataSet and Binding it to 2 TextBoxes, this happens onFormLoad:

// SQLDataAdapter.Fill(DataSet) //

2. I have set 2 navigation buttons that allow me to move back and forth through records.

3. Once I edit the content of a TextBox, I want to be able to update the DataSet, and DataSource at the click of a button in order to commit the changes. I've been trying to use the following, without any possitive results:

// SQLDataAdapter.Update(DataSet) //

4. Once I close the application, and reopen it the records loaded are not up-to-date. Am I missing something here?
Your help will be extremely appreciated.

Thanks!
Miguel Pereira.-

Miguel Pereira
Friday, March 21, 2003

Have you specified the UpdateCommand, InsertCommand, and DeleteCommand properties of the DataAdapter?

Mike Gunderloy
Friday, March 21, 2003

If the update command wasn't set, he'd be getting
runtime error, which he probably isn't (or I presume he
would've mentioned that!). 

How are you binding the text boxes?  Are you binding them
to a particular table in the dataset? Try passing that table to the Update method of the adapter instead of the whole
dataset. Also try the GetChanges method of the table and
see if it returns any rows.

Mike
Saturday, March 22, 2003

I think I know what it /might/ be -- call
  BindingContext[dataSet].EndCurrentEdit();
before doing the Update. Depending on your app, the values might still be languishing in the controls and the DataSet doesn't know of the changes.

Duncan Smart
Sunday, March 23, 2003

Good point, EndCurrentEdit is probably it. Speaking of which, what exactly does it do? I haven't seen a satisfying explanation anywhere. My uderstanding is that it marks the row as either Modified or Added, but shouldn't the binding context be smart enough to figure it out in most cases ?  Say I change the value in a bound textbox and then navigate off the record, shouldn't the binding context realize the change and mark the row as Modified, so when I call Update, it knows to send the changes to the DB ?

Mike
Sunday, March 23, 2003

If there's a move off the record then EndCurrentEdit occurs implicitly. It's required when, say the focus is on a textbox and the user clicks on the Update/save button. When the button's handler is invoked the TextBox hasn't told the BindingContext that the value has changed, so that particular modification hasn't made it to the DataSet yet. Calling EndCurrentEdit ensures all the latest data has been pulled from the controls.

Duncan Smart
Sunday, March 23, 2003

Mike, you just made an excellent point there. I did the following:
Added a control to move my position through the DS records using

    Me.BindingContext("ds", "table").properties.... +=1

[This is what I remember, I am not too mindfull of full code syntax yet]

And after passing from one record to another, I used the:

    SQLDataAdapter.Update(ds)

And guess what?, it does update the DataSource. Answering the question of how Am I binding the DS with the TextBox, I use the DataSource poperty of the TextBox object. Now thanks to you, I know of the EndCurrentEdit method, which is (I think) exactly what the record browsing was achieving behind the scenes!

Thank you so much, now I have to give it a try!
Sincerely,
Miguel.-

Miguel Pereira
Monday, March 24, 2003

It wasn't me, it was Duncan who pointed out the
EndCurentEdit, all I did was ask more questions...

So, I refuse to take the credit...:)

Mike
Thursday, March 27, 2003

*  Recent Topics

*  Fog Creek Home