Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Most efficient method of binding controls

Status
Not open for further replies.

DatabaseDude

Programmer
Nov 7, 2005
112
US
Which is the more efficient method of binding controls (listbox, dropdown list, gridview, etc)?

- Using a SQLDataSource control for each
- Creating a single connection in the codebehind page & using it to retrieve all data

My first instinct is to code everything by hand, but I wanted to take advantage of any built-in power of ASP.NET.

Thanks in advance!
 
if you're using the markup then the sqldatasource will take care of the db connections, just pass the connection string to the object.

if you use the code behind create 1 connection. then use multiple command objects to generate the queries. populate datatables with the results of each command. then close/dispose of the connection.

then bind each datatable to their respective control.

i thing to note: the more markup/codebehind you use the more difficult it is to test the code using automation/unit tests. if you don't use unit tests/automation then this isn't a issue.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
if you don't use unit tests/automation then this isn't a issue.

I disagree. The ability to unit test is actually one of the lesser important reasons for code separation. More important is organization and maintainability.

The SqlDataSource requires the least amount of and is preferable to hard-coding data access logic in the code-beside, but a more elegant and maintainable approach for a larger application is to create a business object that encapsulates the data access logic, then use an ObjectDataSource to bind to your controls.

The reason for this is maintainability: with the business object you write the code once and can reuse it in twenty pages. With the SqlDataSource, you'd have to create the same code twenty times (by walking through a wizard 20 times). Additionally, if you change the data access code, a business object will give you a single place to make the change, vs. having to make the change 20 times for every page that uses the SqlDataSource.

Likewise, if you had to do some processing on the data, the business object makes this easy, vs. having to cut and paste the processing logic to twenty different pages.

You get the idea.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top