I am simply trying to be able to save all information on the form by an ID Number into Arrays & ArrayLists. Then, be able to Retrieve that order by it's ID Number. I am able to save an order, but when I go to Retrieve, I get this error:
I have looked through all related classes/functions/subs...etc. But cannot find where this error is coming from. I believe since exceptions are thrown, Visual Basic does not identify exactly which line the error occurs in the Output Window.
Code for "Save" and "Retrieve" Buttons on Order Form
Public Class frmChild
Private controller As clsController
Private itemsMenu() As String
Private itemsPrices() As String
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
'Save the order
'We are going to use a combination of ArrayLists and Arrays to pass
'All the order information to the business logic layer
'By using this approach we use simple data structure to
'communicate between our objects
'Create an Arraylist to hold all the order Info
Dim orderList As New ArrayList
Try
orderList.Add(txtID.Text) 'Add Order ID
orderList.Add(txtName.Text) 'Add Customer Name
orderList.Add(txtPhone.Text) 'Add Phone Number
orderList.Add(txtDate.Text) 'Add Date
'Create an ArrayList to hold all the order detail info
'for the first 3 lines
Dim detailList As New ArrayList
For i As Integer = 0 To 2
'Create an Array to hold the information for each line item
'Line Number, Item Description, Qty, Price
Dim arrDetailItems(3) As String
arrDetailItems(0) = Me.Panel1.Controls("lblLine" & (i + 1)).Text
arrDetailItems(1) = Me.Panel1.Controls("cboItem" & (i + 1)).Text
arrDetailItems(2) = Me.Panel1.Controls("txtQty" & (i + 1)).Text
arrDetailItems(3) = Me.Panel1.Controls("txtPrice" & (i + 1)).Text
'Add the array to our Detail arraylist
detailList.Add(arrDetailItems)
Next
'Add the detail arraylist to the order arraylist
orderList.Add(detailList)
'Save the order
controller.Save(orderList)
'Check for errors
If controller.getError <> "" Then
'Display the list of all errors
MessageBox.Show(controller.getError, "Error saving order", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
'Loop through each detail line
'and retrieve the total for that line
'use the order ID and the line ID to identify
'the order and the line item
For i As Integer = 0 To 2
Me.Panel1.Controls("lblTotal" & (i + 1)).Text = FormatCurrency(controller.getLineTotal(txtID.Text, i + 1))
Next
'=== Add code below here to display totals
txtTotal.Text = FormatCurrency(controller.getTotal(txtID.Text))
End If
Catch ex As Exception
'Anything else
MessageBox.Show(ex.Message, "Error Saving Item", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
'Retrieve the order
Dim orderinfo As ArrayList
Dim orderDetailList As ArrayList
Try
orderinfo = controller.Retrieve(txtID.Text)
txtID.Text = orderinfo(0)
txtName.Text = orderinfo(1)
txtPhone.Text = orderinfo(2)
txtDate.Text = orderinfo(3)
orderDetailList = orderinfo(3)
Dim arrDetail As String()
For i As Integer = 0 To orderDetailList.Count - 1
arrDetail = orderDetailList.Item(i)
Me.Panel1.Controls("cboItem" & (i + 1)).Text = arrDetail(1)
Me.Panel1.Controls("txtQty" & (i + 1)).Text = arrDetail(2)
Me.Panel1.Controls("txtPrice" & (i + 1)).Text = FormatCurrency(arrDetail(3))
Me.Panel1.Controls("lblTotal" & (i + 1)).Text = FormatCurrency(arrDetail(4))
Next
txtTotal.Text = FormatCurrency(orderinfo(3))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
CODE FOR "SAVE()"
Public Class clsController
'A comment for this class goes here.
'The comment should describe the purpose of
'the class and anthing else that is relevant
'for future development.
'Please comment all your Subs / Functions / Variable Declaration
'appropriately
'Collection to hold our orders
Private colOrders As New Hashtable
Private anOrder As clsOrder
Private strError As String
Public Sub Save(ByVal OrderInfoList As ArrayList)
'Check to see if order is already saved
'If that's the case exit
If colOrders.Contains(OrderInfoList(0)) Then
addError("ID: Duplicate Order ID (Order exists already!)")
Exit Sub
End If
'Create an order object an Order
Dim order As New clsOrder
'Clear the Class error variable
strError = ""
Try
'Store the Order
order.ID = OrderInfoList(0) '0
order.CustomerName = OrderInfoList(1) '1
order.PhoneNumber = OrderInfoList(2) '2
order.OrderDate = OrderInfoList(3) '3
'Store the detail
order.AddDetail(OrderInfoList(4)) '4
'Check for errors
If order.getError = "" Then
colOrders.Add(order.ID, order)
Else
addError(order.getError)
End If
Catch ex As Exception
'Add anything we haven't handled to our
'Class error message
addError(ex.Message)
End Try
End Sub
CODE FOR "RETRIEVE()"
Public Function Retrieve(ByVal anID As String) As ArrayList
'Retrieve an Order
Dim order As New clsOrder
Dim orderInfo As New ArrayList
Try
'get the order object
order = colOrders.Item(anID)
'retrieve the order header info
orderInfo.Add(order.ID)
orderInfo.Add(order.CustomerName)
orderInfo.Add(order.PhoneNumber)
orderInfo.Add(order.OrderDate)
'retrieve the order detail info
orderInfo.Add(order.GetDetail())
'retrieves the order total
orderInfo.Add(order.getTotal())
Catch ex As Exception
'If there is not matching order
'return an error
If IsNothing(order) Then
Throw New ApplicationException("Item not found, ID: " & anID)
'Else
' 'Any other error push back up the stack
' Throw
End If
End Try
Return orderInfo
End Function