Click here to Skip to main content
13,868,893 members
Rate this:
Please Sign up or sign in to vote.
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 8:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190214.1 | Last Updated 10 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100