Please navigate to the bottom of the page for Table of Contents

Thursday, June 2, 2011

Explain ASP.NET data binding using DataSets and DataSourceControls

In a data centric web application, you are required to bind data to a UI control (such as GridView or Repeater). This data can come from a variety of sources. In an interview, it is important for you to know a couple of simple ways to bind data (from say SQL Server table) to a web UI control. This is a very complex topic and there are entire books written on this subject. In this post we will explore two very basic ways to bind data from SQL to the web control.

Using DataSet to bind data

In the first example, we will explore an old way of bind data using DataSets. You will still find this code in many established (legacy) code bases and it is worthwhile to dig deeper in this style. The basic idea is that you get your data in a DataSet and then set that data to the DataSource prperty of your Data bound control and then call DataBind(). The code snippet below shows a simple ASPX page and it’s code behind:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>


The code behind file loads data from the SQL into a DataSet and then binds it to the control.


using System;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = "some valid conn string";
SqlConnection connection = new SqlConnection(connString);

using (SqlCommand cmd = new SqlCommand("Select * from Products", connection))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}


Using Data Source controls to bind data


The other method of displaying data is using the Data Source controls. ASP.NET provides seven built-in data source controls; each targeting a specific data type. I urge you to explore and understand each of the following controls: SqlDataSource, LinqDataSource, ObjectDataSource, XmlDataSource, and so on. These controls can be configured extensively which is beyond the scope of this post. The example below illustrates the simplest use of a SqlDataSource with a GridView control to display data.


The code snippet below shows a very simple declarative way of defining the SqlDataSource.


<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * FROM [Products]"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" />


The same GridView we difined in our first example can be used with slight modifications. Instead of using DataSource and BindData() calls, we just use the DataSourceID property of the control.

 

<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" >
</asp:GridView>


We have barely scratched the surface of Data binding. But this should give a good base for preparing for your interview skills for data binding.

No comments:

Post a Comment