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

Bind Data In a GridView in ASP.NET

Article:
Viewed:  1122 
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 asp.net 

In this I am going to show how we can show our database data in a GridView control in asp.net. 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Display Data In GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="2" cellspacing="2" width="50%" align="center">
            <tr>
                <td width="100" align="right" bgcolor="#eeeeee">
                    Employee Data Using the GridView Control:
                </td>
                <td align="center" bgcolor="#FFFFFF">
                    <asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" Width="100%">
                        <Columns>
                            <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" />
                        </Columns>
                    </asp:GridView>
                    <asp:Label ID="lblStatus" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 

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)
    {
        BindData();
    } 

    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);
        try
        {
            cmd.Connection.Open();
            gvwExample.DataSource = cmd.ExecuteReader();
            gvwExample.DataBind();
            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
    }
}

 

This is my database table structure..

1.png

Figure 1.

 This is when you run the application..

2.png

Figure 2.

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