Return List<T> from Database using C# and VB.Net

Last Reply on Dec 10, 2018 02:13 AM By dharmendr

Posted on Dec 10, 2018 12:49 AM

Hello everyone, I have a database table to store my receipt info and on my receipt I have a list of products on the receipt. To store a list I read I need to make another table of that list data and to link the receipt and list of my products together I’m supposed to link with receiptid. To retrieve the productlist table I have receipt id in the table but in my product class I do not have receipt id do I have a receipt object and product object and properties off both of them classes in 1 new List<T> I wanted to return a new class of that list<T> do get my data.

I made a new class and have a receipt object and product object in it to return properties of both is this the correct way I should be doing this? Any info / help / tips would be greatly appreciated.

 

//I am still in the processing of finishing this method
        public static List<ReturnReceiptProductList> GetReceiptProductList(int rid)
        {
            List<ReturnReceiptProductList> productList = new List<ReturnReceiptProductList>();
            ReturnReceiptProductList returnReceipt = new ReturnReceiptProductList();
            using (SqlConnection connection = ConnectionString.GetSqlConnection())
            {
                string sqlList = "SELECT * FROM OrdersList WHERE ReceiptId = @rid";
                using (SqlCommand cmdList = new SqlCommand(sqlList, connection))
                {
                    cmdList.Parameters.AddWithValue("@rid", rid);
                    connection.Open();
                    using (SqlDataReader reader = cmdList.ExecuteReader())
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                Product product = new Product();
                                Receipt receipt = new Receipt();
                                receipt.ReceiptID = Convert.ToInt32(reader["ReceiptId"].ToString());
                                receipt.UserId = Convert.ToInt32(reader["UserId"].ToString());
                                product.ProductId = Convert.ToInt32(reader["ProductId"].ToString());
                                product.ProductName = reader["ProductName"].ToString();
                                product.ProductQuantity = Convert.ToInt32(reader["ProductQuantity"].ToString());
                                product.ProductPrice = Convert.ToDecimal(reader["ProductPrice"].ToString());
                                product.ProductTax = Convert.ToDecimal(reader["ProductTax"].ToString());
                            }
                        }
                    }
                }
            }
        }
    public class ReturnReceiptProductList
    {
        //Returning the values to link Product & Receipt.

        public Product Product { get; set; }

        public Receipt Receipt { get; set; }

        public ReturnReceiptProductList() { }

        public ReturnReceiptProductList(Product p, Receipt r)
        {
            Product = p;
            Receipt = r;
        }
    }

 

This question does not have replies that have been liked.