PDC Observations, "Round tripping" the data with simple data binding back to the data base

Well, PDC has started. We had a great pre-conference session on VSTO that was well attended--it was fun to show the almost final builds of VSTO 2005 knowing that we're almost to the end of this long journey to get VSTO 2005 released. I can't wait for everyone to be able to start building Office solutions using VSTO when Visual Studio 2005 ships later this year.

One "technical" observation in this blog. As part of preparing the VSTO pre-conference, I did a demo on getting the data from a database, into a data-bound named range in Excel (or bookmark in Word). This is very easy to do through the data tools in VSTO--it is easy to get the data in "one-way" into the document or workbook. But to get the written data back from Excel or Word to the database (presuming you want the user to be able to edit the data and push it back to the database) requires some extra steps.

First of all, you need to do a "WriteValue" on any simple data-bound named range or bookmarks. You do this by writing code like this. You typically want to write this code behind a change event for the named range or at least at some point before you are going to write back to the database:

myNamedRange1.DataBindings.Item(0).WriteValue()

This gets the changed data from the named range in this case back to the dataset.

Then you need to tell the dataset that you are bound to to accept changes made to it (because of WriteValue) by calling AcceptChanges.

MyDataSet.AcceptChanges()

Alternatively, if you are using a BindingSource object, you can call EndEdit on the BindingSource object.

Finally, now that the change has been "WriteValue-d" back to the data set from the data-bound named range, and the change has been "Accepted" by the data set, you need to write the modified data set back to the actual database by using the table adapter that was used to fill the data set and calling Update on the table adapter:

Me.MyTableAdapter.Update(Me.MyDataSet.MyTable)

I was reminded of all of this because I was doing these steps to get data back to the database but forgot the "AcceptChanges" or "EndEdit" step and my database wasn't getting updated properly. Note that you don't have to do the WriteValue and AcceptChanges steps if you are using an Excel List object--it takes care of these two steps automatically because it is doing "complex-databinding" rather than simple data binding.

In other news, I finally got to hold the actual "tree-version" of the VSTO book on Sunday afternoon after a year of work on it. As my editor, Joan Murray said--"It's a big baby"--its 1008 pages, but I'm happy that it still fairly comfortable to hold and read. And yes, I had to read my own book to prepare for the VSTO pre-conference--it is amazing how fast your brain forgets things once you've put them on paper. I had to go back and review some of the more obscure areas of VSTO before I presented on them.

One interesting thing that happened during the VSTO pre conference was the L.A. blackout. Fortunately, there was emergency lighting so we were still able to continue the session and everyone attending the pre conference had a lot of great questions that we were able to answer until the lights came back on. The blackout extended the VSTO session to almost 7 PM, but everyone was gracious about it--albeit tired.

And just as VSTO 2005 is almost ready to ship, we're already talking about the new stuff--we're talking about Office 12 and VSTO 3.0 on the show floor. It is fun to show the new Office 12 features and VSTO support for them although it is kind of wierd to already be talking future stuff when VSTO 2.0 is still technically "future" until it hits the street. I'll try to blog more about exactly what that is all about later this week.