views:

32

answers:

3

I'm trying to make a simple form that contains textboxes that draw from a db in my project. I'm using the table adapter's GetData() method in the xsd file to populate the data context. I want to update one of these textboxes and have the changes reflected in the database. The textboxes populate fine, but the changes don't make it back to the db.

I have this code in my constructor for the window class

        table = adapter.GetData();
        this.DataContext = table;

And in the xaml, I'm binding to a listbox

<ListBox Name="lstItems" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=name, Mode=TwoWay,
                    UpdateSourceTrigger=LostFocus}">                        
                </TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

I've been messing around with this for a while and just can't get it. It seems like it should be fairly simple. Am I incorrectly assuming that wpf can even do this type of databinding? Let me know if you need to know anything else. Thanks.

A: 

The changes should be reflected in "table.name".

I don't know what the "adapter.GetData()" is returning in this situation, but it is likely that you will need to tell the table to persist its changes in order to see them in the DB.

Reed Copsey
+2  A: 

adapter.GetData() will return a DataSet containing the data from the database. This dataset is detached from your database, i.e., changes made to the dataset do not automatically propagate back to the database.

To save the changes back to the database, you can use the Update method of your DataAdapter.

So, in fact, your problem has nothing to do with WPF: This is just how database access through ADO.NET data adapters works.

Heinzi
Okay, thanks for clearing that up. I'm not too familiar with all the types of data access methods out there, but is there a method that is closer to what I'm imagining?
wangburger
*Entity Framework* seems to be the "state of the art" method for WPF data binding, but I don't have personal experience with it. Anyway, I think you also need to explicitly call a "save changes" command there.
Heinzi
+1  A: 

Your adapter is getting the data - adapter.GetData() - and your text box is setting the value in your data - table.name as @Reed says. The data returned is a representation of your data in the database. Think of it like someone asking for a report from the db and getting handed a bunch of paper reports.

However, there's nothing triggering your data to be saved to the db. You probably want something to save the data in the same way that you get it.

Since your text box could either change when you change focus or even when you type a letter (by default it's by focus) you probably want something else to save the data. Do you really want to save it to the db every keystroke?

WPF does the binding to the objects, but not ordinarily to the database.

If you really want it to save when you change focus then you could react to that event. Otherwise, add a "Save" button, to be used once the forms have been filled in.

Lunivore