Programming the ASP.NET 2.0 SqlDataSource Control
(Page 1 of 4 )
Many of us think that Data-Source controls, like the SqlDataSource control, have been developed to work with Data-Bound controls. Yes this is correct and this is exactly the way we should be using it. But in this article, I'm going to show you that you can use the SqlDataSource control without an associated Data-Bound control like the GridView control.
Today we are going to write C# code in the Page_Load() event handler method of the Default.aspx page. It creates a SqlDataSource object and assigns appropriate values for the control's required properties. These properties are used by the SqlDataSource control to communicate with the database, and then call the appropriate methods. That will help you to understand the difference between the code that we write today and the code that was generated by Visual Studio.NET 2005 in the last article, "Introduction to the ASP.NET 2.0 SqlDataSource Control." In that article, we used the SqlDataSource control's Task Menu to make the configuration for the control. The code we write today will help you understand how the SqlDataSource control works that way, so let's start.
Create a new web site, then add a new Web Form to it and write the following code in place of the code of the Default.aspx.cs file. Note: don't add any controls to the markup.
using System;
using System.Data;
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;
using System.Collections;
using System.Data.Common;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
this.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = "Data Source=(local);Initial
Catalog=Northwind;Integrated Security=True";
SqlDataSource1.SelectCommand = "SELECT EmployeeID, FirstName,
LastName, Title FROM Employees";
IEnumerable iteratorObject = SqlDataSource1.Select
(DataSourceSelectArguments.Empty);
HtmlTable table = new HtmlTable();
if (SqlDataSource1.DataSourceMode ==
SqlDataSourceMode.DataReader)
{
foreach (DbDataRecord record in iteratorObject)
{
HtmlTableRow row = new HtmlTableRow();
for (int i = 0; i < record.FieldCount; i++)
{
HtmlTableCell cell = new HtmlTableCell();
cell.Width = "60";
cell.InnerHtml = "<b>" + record[i].ToString() + "</b>";
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
}
else if (SqlDataSource1.DataSourceMode ==
SqlDataSourceMode.DataSet)
{
foreach (DataRowView record in iteratorObject)
{
HtmlTableRow row = new HtmlTableRow();
for (int i = 0; i < record.Row.Table.Columns.Count; i++)
{
HtmlTableCell cell = new HtmlTableCell();
cell.Width = "60";
cell.InnerHtml = "<b>" + record[i].ToString() + "</b>";
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
}
this.Controls.Add(table);
}
}
Run the page and you will get the result shown in the following screenshot.

As you can see, we have accessed the database and retrieved data without writing any ADO.NET data access code. Let's see how we have accomplished this result.
Next: Using the SqlDataSource as our data access mechanism >>
More ASP.NET Articles
More By Michael Youssef