Nested BSON Lists

Jfisher387

Member
Joined
Dec 14, 2022
Messages
21
Programming Experience
Beginner
Hello all,

I have been working on a inventory system, and currently I'm working on creating orders for miscellaneous items that have fallen below a minimum stock Qty.

To keep it short, I have a DGV that auto populates with items below their stock qty. when ready I have a button to click that should take all those items and add them to a list, along with a PO #, order date, and a total price for the order.

I'm saving the lists to a MongoDB as a document.

Here is some code and structure to what I'm doing.

Classes to structure the Data:
 }
    public class OrderItem
    {
        public ObjectId Id { get; set; }

        [BsonElement("Type")]
        public string Type { get; set; }

        [BsonElement("Vendor")]
        public string Vendor { get; set; }

        [BsonElement("Description")]
        public string Description { get; set; }

        [BsonElement("Cost")]
        public double Cost { get; set; }

        [BsonElement("Order Qty")]
        public int OrderQty { get; set; }

        [BsonElement("Serial")]
        public int Serial { get; set; }

        [BsonElement("PID")]
        public string PID { get; set; }
    }
    public class Order
    {

        public Order(List<OrderItem> listOfItems, string po, double orderTotal, DateTime timeOfOrder)
        {
            listOfItems = OrderItemList;
            po = Po;
            orderTotal = OrderTotal;
            timeOfOrder = OrderDate;
        }
        public ObjectId Id { get; set; }

        [BsonElement("Order")]
        public List<OrderItem> OrderItemList { get; set; }

        [BsonElement("PO")]
        public string Po { get; set; }

        [BsonElement("Order Total")]
        public double OrderTotal { get; set; }
        

        [BsonElement("Date")]
        public DateTime OrderDate { get; set; }


    }

Implementation of the structured classes:
OrderItem item = new OrderItem();
List<OrderItem> listOfItemsT = new List<OrderItem>();
foreach (DataGridViewRow row in DGV_orders.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        tbl.AddCell(Convert.ToString(cell.Value));
    }
    item.Description = Convert.ToString(row.Cells["Description"].Value);
    item.Vendor = Convert.ToString(row.Cells["Vendor"].Value);
    item.Cost = Convert.ToDouble(row.Cells["Cost"].Value);
    item.OrderQty = Convert.ToInt32(row.Cells["OrderQty"].Value);
    item.Type = Convert.ToString(row.Cells["Type"].Value);
    item.PID = Convert.ToString(row.Cells["ProductId"].Value);
    item.Serial = Convert.ToInt32(row.Cells["ProductLink"].Value);

    double cost = Convert.ToDouble(row.Cells[1].Value);
    double qty = Convert.ToDouble(row.Cells[2].Value);

    double lineTotal = cost * qty;
    sum += lineTotal;

    listOfItemsT.Add(item);
}
Order order = new Order(listOfItemsT,orderPO.Text, Math.Round(sum, 2),DateTime.Today);

ordersCollection.InsertOne(order);

Below is a flow chart describing how i expect the data to be structured. Hoping this helps visualize what im trying to accomplish.

Screenshot_180.png



Right now as it stands, if I try to create a order I end up with empty document in my database as pictured below.
Screenshot_181.png


I have however gotten the DB to work with my code in other cases. I can add tools, and remove tools through my application and it will affect the database accordingly, as well as reading data in from the database.


I believe my issue lies with the structure of my data. Mainly being that I want a list of nested objects, nested within another tier. I believe the Database handles this just fine, I am just having trouble understanding it.
 
Look closely at your lines 32-35.

Assignments in C# are destination = source, but it looks like you are doing source = destination.
 
Look closely at your lines 32-35.

Assignments in C# are destination = source, but it looks like you are doing source = destination.
And just like that it works. Wow, thanks. I have been scratching my head, rearranging these few lines of code for a couple days now trying to get it right! Appreciate you taking the time to look at it for me!
 
Back
Top Bottom