Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Binding data with a data source control

A data source control moves the connection and command details out of your C# and into the page markup, where a grid can bind to them directly.

The pairing

Markup
<asp:SqlDataSource id="productsSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:Shop %>"
    SelectCommand="SELECT Id, Name, Price FROM Product" />

<asp:GridView id="productsGrid" runat="server"
    DataSourceID="productsSource" AutoGenerateColumns="false" />

What you gain

  • No connection code in the page class.
  • Built in support for sorting, paging and parameters.
  • The query is visible in the markup, which is convenient for small pages.
Keep it in perspective. A data source control is excellent for straightforward screens. When the logic grows, move it into a class where it can be tested, which is the approach used in web services and reuse.

Add sorting next in letting the visitor sort the grid.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.