OverStacked
OverStacked

Reputation: 31

C# bound textbox will not update using tableadapter update

I am having problems getting a dataset to update changes to a database.

I start by filling a dataset using a FillBy query that I created on a table adapter. "SELECT * FROM tblTest WHERE testID = (testID = @testId)

That will return a single record. I want to bind each column of the dataset.datatable to its own textbox on the form.

textBox1.DataBindings.Add("Text", dataset.datatable, dataset.datatable.NameColumn.ToString());

This loads the data fine. I make some changes to the data in the textbox. Then when I use the tableadapter.Update(dataset) to update the changes to the database, it does not update the record.

If I populate a datagrid with this dataset, make changes, and then run the tableadapter.update(dataset), it will update the record.

How can I use the tableadapter.update() without using the datagrid? Do I have to write a custom update for the tableadapter?

Upvotes: 2

Views: 2997

Answers (1)

OverStacked
OverStacked

Reputation: 31

I did find out why the database was not updating. I checked the row state of the changed row and it was still in a "unmodified" state.

I changed the binding setup and created a new bindingsource with the datasource set as the dataset. and the datamember set as dataset.datatable. Then bound the control to a column in that bindingsource.

Before running the tableadapter.update(dataset) the following line is needed so that the row state changes

this.BindingSource.EndEdit(); 

Everything then updated properly

Upvotes: 1

Related Questions