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:
Create a connection string and a command string.
Pass the strings to the constructor of the
SqlDataAdapter
.Create an instance of a
DataSet
.Ask the
DataAdapter
to fill theDataSet
.Extract the table from the
DataSet
.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 ...
Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.