Click here to Skip to main content
15,743,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
                    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;

What I have tried:

linking a table to a table of a list
and many other ways
Updated 10-Dec-18 7:21am

1 solution

You can also use LINQ, this will take some study but provides a lot of power and flexibility. See example here: Simple LINQ to SQL in C#[^]
And here: Querying SQL Server databases using LINQ to SQL[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900