views:

366

answers:

4

I've been a C# programmer for about 2 years total, and professionally for a little more than 1. I work at a company as a developer on an application that began before the days of .NET 2. My question is this:

What is the benefit to use databinding from sql queries directly to a control over querying and manually adding items to the control? Is this approach generally considered cleaner, and simpler in terms of deploying? Will there still be cases where manually adding would give more fine grained control over the display / value of the data?

I'm asking because I've been assigned some bugs that deal with some controls that populate themselves based off query results, and would love to clean up unnecessary logic and push that off to the libraries to handle instead of me.

Note: We are using .NET 2.0

+5  A: 

I personally find that using the

control.DataSource = YourSource;
control.DataBind();

process is much easier, you don't have to do the iteration, and overall reduces LOC.

If working with DropDownLists and other controls you will most likely set the DataValueField and DataTextField properties as well.

Mitchel Sellers
+1  A: 

Data binding is much easier to set up, less error prone overall, reduces LOC significantly (as Mitchel Sellers said), and, a few minor glitches aside, works fairly reliably.

In my experience, you only actually need full manual control if you need to specify the exact update order or timing for data bound controls.

Alan
A: 

It can be useful to manually bind if you've got a complex input scenario. With databound input controls you can find that you only know you've got bad data when it hits the DB and throws an exception (badly formatted date/time, integer out of correct range etc).

You can obviously handle this with the various validation / pre-commit events on the data controls but it can be easier (and more obviously readable) to just manually validate your input and post it when you know it's correct.

That's the only reason I can think of and it's only applicable for input. If you're in a read-only scenario then databinding is a no-brainer.

Marc
A: 

My experiences were quite the opposite to my previous posters here. This blog entry (dated 2006) summarizes my feelings about databinding.

Uwe Keim