Tuesday, 3 January 2012

Select Data From Database And Show in a Gridview using Generic

Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GenericGriedview.aspx.cs" Inherits="GenericGriedview" %>

<!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 id="Head1" runat="server">
<title>Bind Generic List to Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

C# Coading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

public partial class GenericGriedview : System.Web.UI.Page
{
    List<UserDetails> objUserDetails = new List<UserDetails>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindUserDetails();
        }
    }
    protected void BindUserDetails()
    {
        objUserDetails = GetUserDetails();
        gvDetails.DataSource = objUserDetails;
        gvDetails.DataBind();
    }
    protected List<UserDetails> GetUserDetails()
    {
        string strConnection = "data source=linku;Database=satya;uid=sa;pwd=satya_1234";

        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from UserInformation", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UserDetails userinfo = new UserDetails();
                userinfo.UserName = dt.Rows[i]["UserName"].ToString();
                userinfo.FirstName = dt.Rows[i]["FirstName"].ToString();
                userinfo.LastName = dt.Rows[i]["LastName"].ToString();
                userinfo.Location = dt.Rows[i]["Lacation"].ToString();
                objUserDetails.Add(userinfo);
            }
        }
        return objUserDetails;
    }
}

class file User_details
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserDetails
/// </summary>
///

    public class UserDetails
    {
        public UserDetails()
    {
        //
        // TODO: Add constructor logic here
        //
    }
        string username = string.Empty;
        string firstname = string.Empty;
        string lastname = string.Empty;
        string location = string.Empty;

        public string UserName
        {
            get { return username; }
            set { username = value; }
        }

        public string FirstName
        {
            get { return firstname; }
            set { firstname = value; }
        }

        public string LastName
        {
            get { return lastname; }
            set { lastname = value; }
        }

        public string Location
        {
            get { return location; }
            set { location = value; }
        }
    }

No comments:

Post a Comment