Binding GridView using SqlDatasource in ASP.NET

With GridView we can display an entire collection of data, add sorting and paging, and perform inline editing.

The SqlDataSource control is the data source control to use if your data is stored in a SQL Server, SQLServer Express, Oracle Server, ODBC data source, OLE DB data source, or Windows SQL CE Database.

Displaying Data with the GridView



  <%@ Page Language="C#" AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="emp_id" DataSourceID="SqlDataSource1">
                <asp:BoundField DataField="emp_id" HeaderText="emp_id" ReadOnly="True"
                    SortExpression="emp_id" />
                <asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
                <asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
                <asp:BoundField DataField="hire_date" HeaderText="hire_date"
                    SortExpression="hire_date" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
            SelectCommand="SELECT top 10 [emp_id], [fname], [lname], [hire_date] FROM [employee]">


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


<?xml version="1.0"?>


    <add name="pubsConnectionString" connectionString="Data Source=spde670;Initial Catalog=pubs;User ID=sa;Password=sql2005" providerName="System.Data.SqlClient"/>

    <compilation debug="true"/>
    <authentication mode="Windows"/>