In this blog we will know how to display records in a list view (web) from the database.

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListView_in_Asp.net._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>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:ListView ID="ListView1" runat="server">

<LayoutTemplate>

<table id="itemPlaceholderContainer" border="1">

<tr>

<th>EmpID</th>

<th>EmpName</th>

<th>Empaddress</th>

<th>Salary</th>

</tr>

<tr runat="server" ID="itemPlaceholder">

</tr>

</table>

</LayoutTemplate>

<ItemTemplate>

<tr>

<td>

<asp:Label ID="lblempid" runat="server" Text='<%# Eval("empid") %>' />

</td>

<td>

<asp:Label ID="lblempname" runat="server" Text='<%# Eval("empname") %>' />

</td>

<td>

<asp:Label ID="lblempaddress" runat="server" Text='<%# Eval("empaddress") %>' />

</td>

<td>

<asp:Label ID="lblempsal" runat="server" Text='<%# Eval("empsal") %>' />

</td>

</tr>

</ItemTemplate>

</asp:ListView>

</div>

</form>

</body>

</html>

using System;

using System.Collections;

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;

namespace ListView_in_Asp.net

{

public partial class _Default : System.Web.UI.Page

{

string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

SqlCommand com;

SqlDataAdapter sqlda;

DataSet ds;

string str;

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection(strConnString);

con.Open();

str = "select * from employee";

com = new SqlCommand(str, con);

sqlda = new SqlDataAdapter(com);

ds = new DataSet();

sqlda.Fill(ds, "employee");

ListView1.DataMember = "employee";

ListView1.DataSource = ds;

ListView1.DataBind();

con.Close();

}

}

}

Thanks for reading