1

In recent attempts to understand the ASP.NET GridView I have seen mst examples make use of a datasource declared directly in the ASPX markup. For example ...

<asp:SqlDataSource ID="productDataSource" Runat="server" 
 SelectCommand="SELECT [ProductName], [UnitPrice], 
 [UnitsInStock], [QuantityPerUnit] FROM [Products]"
    ConnectionString=
    "<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>

I have seen a lot this sort of declarative datasource as opposed to the way I was taught (and personally prefer) of using ADO.NET to access data and then set the controls datasource in the code-behind.

Is there an advantage to using this new type of datasource declaration in the ASPX? What are the pros and cons?

webworm
  • 10,587
  • 33
  • 120
  • 217

2 Answers2

3

One disadvantage of the declarative way of doing the data binding is that the paging is not efficient. The gridview will pull all records from the database, and then it will only show you your page. If you have 1,000,000 records, that is not going to be fast. Doing the binding yourself, you can only pull the records you need.

For simple and small tables, the declarative method requires less development time, and the paging and sorting are built in.

For almost all real-world gridviews I have worked on, the complication of the queries to build the data set has made the declarative method not feasible.

Zach Green
  • 3,421
  • 4
  • 29
  • 32
  • Additionally, I find maintaining these to be a pain. I used these on one project and never will again. – joelt Mar 16 '11 at 01:47
  • Based upon your experiences would you just use the DataGrid control even though would require some "manual" coding? – webworm Mar 16 '11 at 03:15
  • Personally, I use the gridview, but MS has a decent listing of the differences to decide what works for you. http://msdn.microsoft.com/en-us/library/05yye6k9.aspx – Zach Green Mar 16 '11 at 04:03
0

Declarative databinding is useful in straight-forward cases, while learning and in demos of course. As soon as you start experining with it go ahead and switch to a code-driven design and save yourself the pain.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186