Hello all,
I am trying to create a nested collection of orders on MongoDB. I have successfully inserted and read data from mongo. However I am having issues with nested data.
Below is the structure of my classes.
public class OrderItem
{
[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 OrderList
{
[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; }
}
and then below is the implementation. i get a object reference not set to an instance of an object as the error.
OrderList order = new OrderList();
OrderItem item = new 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;
order.OrderItemList.Add(item);
}
order.OrderTotal = Math.Round(sum, 2);
order.OrderDate = DateTime.Today;
order.Po = orderPO.Text;
ordersCollection.InsertOne(order);
does anyone have any helpful ideas what i’m doing wrong?
What i’m going for is a list of orders, with each order containing a total, a data, a PO, and then a list of items on the order. I think i’m very close but no luck yet.