efficiently update datasouce from form with many textboxes

R

Randy

I have a form with 36 text boxes on it. I have everything working for
the user to make changes to existing records or to add new ones. When
I come to the point of passing this data on to the data source, I'm
looking for an efficient way to do this. The only way that I know of
is a line-by-line procedure that passes each textbox to the
appropriate field in the data table one-by-one. Example:

Dim drNew As DataRow

drNew = dsMyDataSet.myTable.NewRow()

drNew.Item("Column1") = TextBox1.text
drNew.Item("Column2") = TextBox2.text
drNew.Item("Column3") = TextBox3.text
(etc, etc, through 36 text boxes)

dsMyDataSet.MyTable.Rows.Add(drNew)
taMyTableAdapter.Update(dsMyDataSet.MyTable)
dsMyDataSet.AcceptChanges()

I'd like to find a loop that would work something like this (more or
less):

Dim c As Control
Dim x as integer

For Each c In Me.Controls
If TypeOf c Is TextBox Then
Dim tb As TextBox = DirectCast(c, TextBox)
x = +1
drNew.Item(x)=c.text
End If
Next

Anybody have any ideas?

Thanks,
Randy
 
R

RobinS

Data bind the textboxes to the data source, and they will be passed
automatically.

Robin S.
 
R

Randy

Data bind the textboxes to the data source, and they will be passed
automatically.

I tried this, but it isn't working as intended. For the textbox that
is supposed to update the primary field, I have its text bound to
IngredientsBindingSource - Ingredients, which follows from the
dataset. Is this the correct place to bind for this purpose? Also,
how do I trigger the data to be passed to the datasource. As there
are many textboxes on this form, I want the user to fill them all in
and then click a Save button, which will trigger the data to be
persisted to the datasource.

Anything else that I need to know? I've been through a couple of
books over the last few weeks and am slowly understanding some of
these ideas. I didn't realize that a bindingsource could be used to
push data all the way back to the source. I thought that it just was
used for the dataset.
 
R

RobinS

You databind each control to a specific field in your table. You can do
this in a multitude of ways. I use business objects, so I do object
binding. If you have a strongly typed dataset, you can show the data source
and just drag from the data source onto your control. Or you can manually
bind behind the scenes.

It sounds like you are showing a data source and dragged it onto the form,
or onto a control. Is that right?

When the user tabs out of a databound control, it pushes the number down
into the dataset in memory. Usually, you do a call to myForm.Validate()
when he hits save, so if he hasn't tabbed out of the last field he changed,
it picks up the change. After the call to Validate, you would call
myAdapter.Save() and it would iterate through the records and save them as
discussed in the other thread.

The binding source is kind of the glue, or lubricant. You don't *have* to
use a binding source. You can bind directly to the data source. However,
using a binding source speeds up the display to the screen, and any changes
to the data source are immediately displayed by the binding source. It also
gives you a bunch of extra fun stuff to play with, like sorting and
filtering.

For some really excellent information on data binding, check out Brian
Noyes' Data Binding book. It's in C#, but the downloadable code is also
available in VB. It was invaluable to me.

Does that help?

Robin S.
----------------------------------
 
C

Cor Ligthert [MVP]

Robin,

Don't you have to do otherthins there in SF, it seems it looks a lot like
Holland (the real Holland). But we don't look how we can win, half a
picosecond.

Cor
 
R

RobinS

I'm not really sure what you mean, Cor. I wasn't telling the guy how to get
better performance, I was trying to figure out what he was talking about
and how to help him.

I haven't had the privilege of visiting Holland, although the pictures I
see of it are beautiful. I think biking along those canals would be really
cool. And isn't it warmer in Holland than it is here? It's like 60-70 in SF
most of the time.

Robin S.
 
C

Cor Ligthert [MVP]

RobinS,

I know that you were telling on better performance, but I was telling you
that I thought that they were used in SF to spent there time an better
things like here in Amsterdam than how to win a picosecond. I haven't been
there too, but about reading from it, it are too people who are very
realistic. Therefore I wrote this, spending your time on pico seconds as we
are doing now is in my opinion ridiculous, I was of course not writting to
you but indirect to the OP. But I thought people from SF can easier
understand why I wrote this.

I assume that the climat in SF is like Amsterdam, when I see pictures people
are dressed the same.

Cor
 
R

RobinS

Hi, Cor,

I actually wasn't talking about getting better performance at all, I was
talking about how to use data binding. I agree with you, trying to make
something an itsy bitsy bit faster usually takes more time than it's worth.

It was 50 deg F here 3 days ago, 97 deg F two days ago, 70 deg F yesterday,
and 65 deg F today. Weird. We can have a 40 deg F swing during the day. I
live about 30 mi. east of SF, in a valley, and our temperatures are a lot
more extreme than the city itself. The company I work for is in SF, but I
am lucky enough to be able to telecommute, and only go in to the city a
couple of days a month.

Some day I will come to Holland; maybe we can get together for a drink. :)

Robin S.
 

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