Hi nauna,
I have created sample please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ListView ID="lvCustomers" runat="server" OnItemDataBound="lvCustomers_ItemDataBound">
<ItemTemplate><b>
<asp:Label ID="lblID" Text='<%# Eval("CustomerID") %>' runat="server" /></b>
<asp:HiddenField ID="hfCustomerID" runat="server" Value='<%# Eval("CustomerID") %>' /><br />
<asp:ListView runat="server" ID="lvOrders" GroupPlaceholderID="orderGroupPlaceHolder"
ItemPlaceholderID="orderItemPlaceHolder">
<LayoutTemplate>
<table>
<tr>
<th>Order ID</th>
<th>Price</th>
</tr>
<asp:PlaceHolder ID="orderGroupPlaceHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr><asp:PlaceHolder ID="orderItemPlaceHolder" runat="server"></asp:PlaceHolder></tr>
</GroupTemplate>
<ItemTemplate>
<td><asp:Label ID="lblID" Text='<%# Eval("OrderID") %>' runat="server" /></td>
<td><asp:Label ID="lblPrice" Text='<%# Eval("Freight") %>' runat="server" /></td>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
Namespaces
C#
using System.Collections.Generic;
using System.Linq;
using NorthwindModel;
VB.Net
Imports System.Collections.Generic
Imports System.Linq
Imports NorthwindModel
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (NorthwindEntities entities = new NorthwindEntities())
{
List<Customer> customers = new List<Customer>();
customers = (from c in entities.Customers
select c).Take(3).ToList();
this.lvCustomers.DataSource = customers;
this.lvCustomers.DataBind();
}
}
}
protected void lvCustomers_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListView lvOrders = (e.Item.FindControl("lvOrders") as ListView);
string id = (e.Item.FindControl("hfCustomerID") as HiddenField).Value;
using (NorthwindEntities entities = new NorthwindEntities())
{
List<Order> orders = new List<Order>();
orders = (from o in entities.Orders
where o.CustomerID == id
select o).Take(2).ToList();
lvOrders.DataSource = orders;
lvOrders.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Using entities As NorthwindEntities = New NorthwindEntities()
Dim customers As List(Of Customer) = New List(Of Customer)()
customers = (From c In entities.Customers Select c).Take(3).ToList()
Me.lvCustomers.DataSource = customers
Me.lvCustomers.DataBind()
End Using
End If
End Sub
Protected Sub lvCustomers_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
Dim lvOrders As ListView = (TryCast(e.Item.FindControl("lvOrders"), ListView))
Dim id As String = (TryCast(e.Item.FindControl("hfCustomerID"), HiddenField)).Value
Using entities As NorthwindEntities = New NorthwindEntities()
Dim orders As List(Of Order) = New List(Of Order)()
orders = (From o In entities.Orders Where o.CustomerID = id Select o).Take(2).ToList()
lvOrders.DataSource = orders
lvOrders.DataBind()
End Using
End Sub
Output