# How to Display Data in list view dynamically



## waseemsafder (Nov 10, 2006)

Dear sir,
When i bind my data source to gridview
It show the data.but when i bind my data source to list view or form view it not show the data.
It works
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionstring);
string cmd = "Select firstname,lastname,age,city,sex From [profile] where username='" + Session["username"] + "'";
SqlDataAdapter sda = new SqlDataAdapter(cmd, connection);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
but 
ListView1.DataSource = ds;
ListView1.DataBind();
not works
how to show data in list view,form view etc


----------



## Fyzbo (Feb 6, 2002)

Will you please post the entire code for your example. Both the html and C#.


----------



## waseemsafder (Nov 10, 2006)

HERE is the .html code File

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Untitled Page

<asp:Label ID="Label1" runat="server" Text="Label" Width="177px"></asp:Label>

Search Friend EditProfile
Login.aspx.cs<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [firstname], [lastname], [age], [city], [sex] FROM [profile] WHERE ([username] = @username)">
<SelectParameters>
<asp:SessionParameter Name="username" SessionField="Session["username"]"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>






<asp:GridView ID="GridView1" runat="server">
</asp:GridView>


here is the .aspx file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class profile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Label1.Text = ("Welcome " + Session["username"]);
string connectionstring = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionstring);
string cmd = "Select firstname,lastname,age,city,sex From [profile] where username='" + Session["username"] + "'";
SqlDataAdapter sda = new SqlDataAdapter(cmd, connection);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch(Exception cc) 
{
Response.Write(cc.Message);

}

}
protected void LoginView1_ViewChanged(object sender, EventArgs e)
{

}
}


----------



## Fyzbo (Feb 6, 2002)

I see a gridview which you said works, but there doesn't seem to be a listview on the page.


----------



## waseemsafder (Nov 10, 2006)

yes there is no listview.Actually in that code i use gridview.If i use listview instead of gridview,no data is displayed in the list view.
Thats what's the problem I have.


----------



## waseemsafder (Nov 10, 2006)

<asp:ListView ID="ListView1" runat="server">
</asp:ListView>
in .aspx file
ListView1.DataSource=ds;
ListView1.Databind();


----------



## Fyzbo (Feb 6, 2002)

Got a bit mixed up. Listview is for windows applications not asp.net webpages. You can look into using a datalist or formview. For those you can not just bind to data the same way you do a gridview, you need to define how the data will be displayed with an itemtemplate.


----------



## waseemsafder (Nov 10, 2006)

yes Thats what I asked?Can you Please give an example


----------



## waseemsafder (Nov 10, 2006)

And one Question From DataBase side 
How to select third highest value in the database?
What is the Sql-query for that?


----------



## Fyzbo (Feb 6, 2002)

Formview documentation: http://msdn2.microsoft.com/en-us/library/fyf1dk77.aspx
FOrmview Example: http://msdn2.microsoft.com/en-us/library/ms227423(VS.85).aspx


----------



## Fyzbo (Feb 6, 2002)

Finding the second highest value, change it to top 1, top 3
http://codeforeternity.com/blogs/te...e-second-highest-column-value-in-a-table.aspx


----------



## waseemsafder (Nov 10, 2006)

thank you very much.Its really working.Thanks again.


----------

