To Bind or not to Bind

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi, Im looking for opinions here rather than actual code.

I am designing an office application to show a clients details, there a number of ways to implement this, I first present a list of clients and the user chooses the particular client and then I show all the clients details about 20 data items on a single form. Currently I do this programmatically using a select query to get a single rows of data and assign them to the control on the Form Load event. When the user changes any control values the user has to click on a Save button to write back the changes to the DB, I programmatically reassign the value from the control back to the dataset row column and run an update query.

Another alternative which I have never used is to bind the client form and controls to the actual client table via the controls setting and then the data will update automatically when the user changes the controls, so no need for the user to save any changes and then using a Record Navigator I can step through each in turn.

Which is best tho, somehow I feel the programming is better as I have more control although it can be tedious to implement, the binding is easier and less time consuming.

What are the advantages of one over the other?

kind regards
 
You say that you present the user with a list of clients up front. How much of each record are you retrieving into that list? Is it all ~20 columns or just a small subset?
 
so no need for the user to save any changes

That isn't true. Changes you make in the UI are automatically propagated to the bound data source but that has no effect on the database. You still need to save changes from the data source back to the database.
 
I can think of a number of ways you might handle this.

1. Retrieve all the data up front. This is probably easiest and won't be a problem unless the amount of data is large. You can populate a DataTable in your first form and bind as appropriate to display just the data they need to see. When it's time to edit, you already have the full record selected so you can pass that to the edit dialogue. I would tend not to bind in the dialogue but you certainly could do if you wanted. The amount of code to setup binding would be about the same as moving data manually. If you don't bind then you simply don't push the data back to the DataTable but if you do bind then you must rollback your changes explicitly by calling RejectChanges on the DataRow. Saving changes would be done in the first form after the dialogue closes.

2. Use a DataTable with all the columns but only pull back the five you need initially. When it's time to edit, pull back the rest of the columns for just the selected record. The rest would be the same as option 1.

3. Populate a DataTable with only the columns you need for the first form and then pass the ID of the record to be edited to the dialogue. The dialogue would use a data reader to get the data for that record and manually populate the controls, then call ExecuteNonQuery to save after manually pulling the data from the controls. I would not use a DataTable and particularly a DataSet for one record.
 
Hi,
Thanks for you reply.

What I asking about tho, it binding using the properties of each Form control to a specific column in a table (the was MS Access does it), IT has the connection string built into it so there no maintenance.

Is binding a good idea, is it more efficient than storing the data in a datatable, what are the downsides ??

kind regards
 
You seem to be under some misconceptions. Binding is not an alternative to using a DataTable. In many cases, a DataTable will be the data source that you bind to your controls. Binding is a good idea in situations where it's appropriate and not when it's not. Whether it's the best option depends on the situation.
 
If so, then why can I go to the properties of a textbox click on the binding property, choose a connection string (or create a new one), and link it to a column on a table?

With no data table created (by me)!

kind regards
 
I've never done it that way but that would almost certainly be creating a DataTable behind the scenes. Even if it's not though, so what? Like I said, data-binding not an alternative to using a DataTable. Even if doing it that way doesn't use a DataTable, you still can bind to a DataTable. Data-binding simply means setting up a relationship between a UI element and a data source such that the data is automatically pushed one way, the other or both. That's not an alternative to using a DataTable. If you use a DataTable at all then it is the data source. Whether your data source is a DataTable or something else, you can either use data-binding or write code to move the data manually. Data-binding is an alternative to that code but you can use a DataTable or not either way.
 
Back
Top