## Help with Packing Math, freight pallets

Th is my first post, I'm new here. I tried the ASP.Net forum, but not a single person answered.

I write software for shopping carts. I just added freight quotes because of the high demand for it. Writing the XML request and response was easy. Little did I know, I would have to reshape, or package items into cube like shapes, to fit them on pallets. I've been working on formula's that I could translate in vb.net, but none of my formulas seem efficient. Each time I add a new scenario, I break my formula and have to start again.

Scenario 1
I have a folding chair that is flat packed in a cardboard box that is
Length: 35
Width: 17
Height: 4

Let's say the customer orders 10 of them. I could take the Length * Qty for
Length : 350
Width: 17
Height: 4
Now I have a long box that won't fit on the truck.

The typical pallet is: L: 44 W: 40 H: 70

If the customer orders 50 chairs, then it spills over to two pallets.

I need help, or a point in the right direction. I've tried doing the math via SQL SELECT SUM(Qty * Length) FROM ShoppingCart WHERE SessionID=@SessionID, but I don't have a formula

I tried Code, where I loop through the QTY and build up the dimensions, but ended up with so much wasted space.

My lastest Code
Do Until Calculated_CubicSize >= Total_CubicSize
If Calculated_CubicSize <= Total_CubicSize Then
If ldx < Max_LengthCount Then
Dim idx As Integer = 0
Total_Width = Total_Width + Width
Do Until ldx = Max_LengthCount - 1 Or CountDown_Qty = 0
CountDown_Qty = CountDown_Qty - 1
Total_Length = Total_Length + Length
Calculated_CubicSize = Calculated_CubicSize + CubicSize
idx += 1
If idx = Max_LengthCount Then
ldx += 1
idx = 0
End If
Loop
ldx = 0
wdx = 1
hdx = 1
End If
Else
Exit Do
End If

If Calculated_CubicSize <= Total_CubicSize Then
If wdx < Max_WidthCount Then
Dim idx As Integer = 0
Do Until wdx = Max_WidthCount Or CountDown_Qty = 0
CountDown_Qty = CountDown_Qty - 1
Calculated_CubicSize = Calculated_CubicSize + CubicSize
Total_Width = Total_Width + Width
wdx += 1
If idx = Max_WidthCount Then
'Total_Width = Total_Width - Width
idx = 0
Else

End If
Loop
wdx = 0
hdx = 1
End If
Else
Exit Do
End If

If Calculated_CubicSize <= Total_CubicSize Then
Dim idx As Integer = 0
Total_Height = Total_Height + Height
Do Until hdx = Max_HeightCount Or CountDown_Qty = 0
CountDown_Qty = CountDown_Qty - 1
Calculated_CubicSize = Calculated_CubicSize + CubicSize
idx += 1
If idx = Max_HeightCount Then
Total_Height = Total_Height + Height
idx = 0
hdx += 1
End If
Loop
Else
Exit Do
End If