ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Return List<T> from Database using C# and VB.Net<p>Hi&nbsp;bigbear,</p> <p>Refer below sample code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Configuration</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { List&lt;ReturnOrderList&gt; orderDetails = GetReceiptProductList(10249); } NorthwindEntities entities = new NorthwindEntities(); public static List&lt;ReturnOrderList&gt; GetReceiptProductList(int id) { List&lt;ReturnOrderList&gt; productList = new List&lt;ReturnOrderList&gt;(); using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { string sqlList = &#34;SELECT * FROM [Order Details] WHERE OrderID = @id&#34;; using (SqlCommand cmdList = new SqlCommand(sqlList, connection)) { cmdList.Parameters.AddWithValue(&#34;@id&#34;, id); connection.Open(); using (SqlDataReader reader = cmdList.ExecuteReader()) { if (reader != null) { while (reader.Read()) { Order_Detail details = new Order_Detail(); details.ProductID = Convert.ToInt32(reader[&#34;ProductID&#34;]); details.UnitPrice = Convert.ToDecimal(reader[&#34;UnitPrice&#34;]); details.Quantity = Convert.ToInt16(reader[&#34;Quantity&#34;]); Order order = new Order(); order.OrderID = Convert.ToInt32(reader[&#34;OrderID&#34;]); ReturnOrderList returnReceipt = new ReturnOrderList(); returnReceipt.Orders = order; returnReceipt.OrderDetails = details; productList.Add(returnReceipt); } } } } } return productList; } public class ReturnOrderList { public Order Orders { get; set; } public Order_Detail OrderDetails { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim orderDetails As List(Of ReturnOrderList) = GetReceiptProductList(10249) End Sub Private entities As NorthwindEntities = New NorthwindEntities() Public Shared Function GetReceiptProductList(ByVal id As Integer) As List(Of ReturnOrderList) Dim productList As List(Of ReturnOrderList) = New List(Of ReturnOrderList)() Using connection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Dim sqlList As String = &#34;SELECT * FROM [Order Details] WHERE OrderID = @id&#34; Using cmdList As SqlCommand = New SqlCommand(sqlList, connection) cmdList.Parameters.AddWithValue(&#34;@id&#34;, id) connection.Open() Using reader As SqlDataReader = cmdList.ExecuteReader() If reader IsNot Nothing Then While reader.Read() Dim details As Order_Detail = New Order_Detail() details.ProductID = Convert.ToInt32(reader(&#34;ProductID&#34;)) details.UnitPrice = Convert.ToDecimal(reader(&#34;UnitPrice&#34;)) details.Quantity = Convert.ToInt16(reader(&#34;Quantity&#34;)) Dim order As Order = New Order() order.OrderID = Convert.ToInt32(reader(&#34;OrderID&#34;)) Dim returnReceipt As ReturnOrderList = New ReturnOrderList() returnReceipt.Orders = order returnReceipt.OrderDetails = details productList.Add(returnReceipt) End While End If End Using End Using End Using Return productList End Function Public Class ReturnOrderList Public Property Orders As Order Public Property OrderDetails As Order_Detail End Class</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/161186/Return-ListT-from-Database-using-C-and-VBNet/https://www.aspforums.net:443/Threads/161186/Return-ListT-from-Database-using-C-and-VBNet/Mon, 10 Dec 2018 02:13:52 GMT