Newbie: Bound textbox not updating Dataset?

D

di

I am new to VB.NET and having some problem with the data binding for
controls. My problem is this:

I want to create a form that has a number of textboxes that relate to fields
in a SQL table. When I update the textboxes, I want SQL to be updated (by
pressing an update button).

I have now read various articles in various books and seem to be getting
nowhere, even when I am creating a test.


My test form has three textboxes, a data adapter ("clientadapter" - added by
the wizard), a "clientconnection" (added by the wizard) and I have generated
a dataset "clientdataset". The connection and adapter is connecting to
Northwind and getting "Select * from Customers".

Each of the textboxes have had the "DataBindings Text" property set to an
appropriate field from the "clientdataset".
On loading the form i run

clientAdapter.fill(clientDataset)

which happily populates the textboxes with information from the database.

I also have a button on the form called btnUpdate, the code behind which is:
clientAdapter.Update(clientDataset)

I was expecting that this update code would go and update the actual
database, however, when I close the application and run it again, the
changes that I have made to the fields are lost.

I am certain that I am missing something fairly simple, but i cannot find it
for the life of me.

I would be very grateful if someone could help me out.

Many thanks
DI


PS: Strangely, when I bind a datagrid to the dataset, the update facility
works as I would expect. It only seems to be causing a problem with the
textboxes.
 
A

Antoine Cote [MSFT]

Hello,

I think the problem that you are seeing is caused by the fact that the data
binding code only pushes the values to the dataset when you navigate to a
different row or call EndCurrentEdit on the binding manager.

Lets say you have a textbox bound to the dataset table like this:
textBox1.DataBindings.Add("Text", myDataSet, "Orders.OrderID"), you can get
to the binding manager like this:

Dim bmb as BindingManagerBase = BindingContext(myDataSet, "Orders")

You could modify the code for your Update button to call
bmb.EndCurrentEdit() before calling Update on the data adapter. This will
tell the binding manager to push the values from the controls to the
underlying datasource.

Note that it is important that you get the binding manager using the same
key/value pair that was used to bind the controls else you'll end up with a
different binding manager. For example, if the textbox was bound like this
instead: textBox1.DataBinding.Add("Text", myDataSet.MyTable, "OrderID")
then you'd need to retrieve the binding manager like this:
BindingContext(myDataSet.MyTable, "")

HTH

Antoine
Microsoft Visual Basic .NET

This reply is provided AS IS, without warranty (express or implied).


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top