Hi ramco1917,
Please refer the below code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<CustomerOrders> orders = this.GetCustomerOrders();
}
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public List<CustomerOrders> GetCustomerOrders()
{
List<CustomerOrders> customerOrders = new List<CustomerOrders>();
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT TOP 2 CustomerId,ContactName,Country FROM Customers ";
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
CustomerOrders customer = new CustomerOrders
{
CustomerId = rdr["CustomerId"].ToString(),
Name = rdr["ContactName"].ToString(),
Country = rdr["Country"].ToString(),
Orders = GetOrders(rdr["CustomerId"].ToString())
};
customerOrders.Add(customer);
}
con.Close();
}
}
return customerOrders;
}
public List<Order> GetOrders(string customerId)
{
List<Order> orders = new List<Order>();
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT TOP 5 OrderId,Freight,ShipCountry FROM Orders WHERE CustomerId = @CustomerId";
cmd.Parameters.AddWithValue("@CustomerId", customerId);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Order order = new Order
{
OrderId = rdr["OrderId"].ToString(),
Freight = rdr["Freight"].ToString(),
ShipCountry = rdr["ShipCountry"].ToString(),
};
orders.Add(order);
}
con.Close();
}
}
return orders;
}
public class CustomerOrders
{
public string CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public string OrderId { get; set; }
public string Freight { get; set; }
public string ShipCountry { get; set; }
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim orders As List(Of CustomerOrders) = Me.GetCustomerOrders()
End Sub
Private strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Public Function GetCustomerOrders() As List(Of CustomerOrders)
Dim customerOrders As List(Of CustomerOrders) = New List(Of CustomerOrders)()
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "SELECT TOP 2 CustomerId,ContactName,Country FROM Customers "
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim customer As CustomerOrders = New CustomerOrders With {
.CustomerId = rdr("CustomerId").ToString(),
.Name = rdr("ContactName").ToString(),
.Country = rdr("Country").ToString(),
.Orders = GetOrders(rdr("CustomerId").ToString())
}
customerOrders.Add(customer)
End While
con.Close()
End Using
End Using
Return customerOrders
End Function
Public Function GetOrders(ByVal customerId As String) As List(Of Order)
Dim orders As List(Of Order) = New List(Of Order)()
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
con.Open()
cmd.CommandText = "SELECT TOP 5 OrderId,Freight,ShipCountry FROM Orders WHERE CustomerId = @CustomerId"
cmd.Parameters.AddWithValue("@CustomerId", customerId)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Dim order As Order = New Order With {
.OrderId = rdr("OrderId").ToString(),
.Freight = rdr("Freight").ToString(),
.ShipCountry = rdr("ShipCountry").ToString()
}
orders.Add(order)
End While
con.Close()
End Using
End Using
Return orders
End Function
Public Class CustomerOrders
Public Property CustomerId As String
Public Property Name As String
Public Property Country As String
Public Property Orders As List(Of Order)
End Class
Public Class Order
Public Property OrderId As String
Public Property Freight As String
Public Property ShipCountry As String
End Class
Screenshot