.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to display records in a listview in Asp.net

Posted By:satyapriyanayak       Posted Date: August 30, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

We will know how to display records in a list view (web) from the database.

<%@ 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>
    <form id="form1" runat="server">
        <asp:ListView ID="ListView1" runat="server">


<table id="itemPlaceholderContainer"  border="1">
<tr runat="server" ID="itemPlaceholder">



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



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



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


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


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);
            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;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend