O'Reilly logo

Programming ASP.NET, 3rd Edition by Dan Hurwitz, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Started with ADO.NET

Create a new web site named SimpleADONetGridView. Drag a GridView onto the page and accept all its default values. Do not attach a data source. Switch to the code-behind file. In the code-behind page, you will create a DataSet and then assign one of the tables from that DataSet to the DataSource property of the GridView.

To get started, add a using statement for the SqlClient namespace to your source code:

    using System.Data.SqlClient;

Tip

You’ll need to add this using statement in all the examples in this chapter.

That done, you will implement the Page_Load method to get the Customers table from the Northwind database and bind it to your GridView. You do this in a series of steps:

  1. Create a connection string and a command string.

  2. Pass the strings to the constructor of the SqlDataAdapter.

  3. Create an instance of a DataSet.

  4. Ask the DataAdapter to fill the DataSet.

  5. Extract the table from the DataSet.

  6. Bind the GridView to that table.

The complete source code for this example is shown in Example 10-1.

Example 10-1. SimpleADONetGridView Default.aspx.csSource

using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // 1. Create the connection string and command string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required