Binding control to SQL and updating

cami

New member
Joined
Sep 15, 2010
Messages
1
Programming Experience
5-10
Hi,
I have controls on a form, bound to an SQL data source.
The information populates the control well.
When I change the text in a bound textbox, I want it to be reflected in the SQL database.

When I change the text in the control, the DataSet is updated.
Now I need to make the DataSet update the SQL table.

I tried:
myTableAdapter.Update(myDataSet.aSpecificTable)

but nothing got updated in the DB.

Binding a control for read and write appears to me to be a standard requirement, but I am having a hard time to find a clear explanation.

Any suggestions?
 
Typing into a bound control doesn't inherently update the data source. Something has to prompt that action. Usually it will be the user navigating to a different control or to a different record, but if that doesn't happen then you have to do it in code. To ensure that all pending changes have been pushed down from the UI to the data source, you should always call EndEdit on your BindingSource before calling Update on your TableAdapter. If you have any validation on your controls then you should call ValidateChildren on the form before calling EndEdit too.

By the way, there's no such thing as a SQL database. SQL is a language used by pretty much all databases, queries and the like. You probably mean SQL Server, in which case you should say SQL Server to distinguish it from MySQL, SQLite and all the other databases with SQL in their name. You're certainly not the only person to do this but is it really worth sacrificing clarity to not type a few extra characters.

Also, your controls aren't bound to a SQL data source. They're bound to a DataSet. How that DataSet gets populated is irrelevant to the data-binding process.

These are subtleties that may not make a difference but an incomplete understanding of the subtleties is where a lot of bugs come from, which is why I like to be precise and recommend that everyone else do the same.
 
Back
Top