Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Bind Data In a GridView in ASP.NET

Viewed:  1168 
Posted On:  02/09/2012 16:41:04 
In this article I am going to show how we can show our data in a GridView in 

In this I am going to show how we can show our database data in a GridView control in How we can make a connection to our database.

This is my aspx code:

<%@ 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">
    <title>Display Data In GridView</title>
    <form id="form1" runat="server">
        <table cellpadding="2" cellspacing="2" width="50%" align="center">
                <td width="100" align="right" bgcolor="#eeeeee">
                    Employee Data Using the GridView Control:
                <td align="center" bgcolor="#FFFFFF">
                    <asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" Width="100%">
                            <asp:BoundField DataField="ID" HeaderText="ID" />
                            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                            <asp:BoundField DataField="JoiningDate" HeaderText="Join Date" />
                    <asp:Label ID="lblStatus" runat="server"></asp:Label>

This is my aspx.cs code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; 

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

    private void BindData()
        SqlConnection con = new SqlConnection("Server=.;Database=MyData;Uid=sa; pwd=wintellect");
        SqlCommand cmd = new SqlCommand("SELECT TOP 5 ID, FirstName,LastName,JoiningDate FROM Employee", con);
            gvwExample.DataSource = cmd.ExecuteReader();
        catch (Exception ex)
            lblStatus.Text = ex.Message;


This is my database table structure..


Figure 1.

 This is when you run the application..


Figure 2.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us