Last post Jul 27, 2016 10:24 PM by PatriceSc
Jul 27, 2016 08:58 PM|dlchase|LINK
We are building a DataTable and then using it in code to search for a labor rate to use in a dataset row. However, when a matching record is not found in the DataTable and we try to refer to a column in the row it fails with the error message below.
"Index was outside the bounds of the array."
Below is a summation of the code.
'Create the DataTable for labor rates to use in lookup on lines
Dim dt As New DataTable()
'Create the columns
Dim dcLaborType As New DataColumn("LaborType", GetType(String))
Dim dcLaborRate As New DataColumn("LaborRate", GetType(String))
Dim dcLaborTax As New DataColumn("LaborTax", GetType(String))
Dim strLaborType As String = ""
Dim strLaborRate As String = ""
Dim strLaborTax As String = ""
'Add the columns to the DataTable's column collection
...filled all rows of DataTable
'get the labor rate
Dim rowRate As DataRow() = dt.Select("LaborType = '" & strModLbrTy & "'")
dblLaborRate = rowRate(0).Item(1) <-- code fails here with error message
Jul 27, 2016 10:24 PM|PatriceSc|LINK
Correct You are trying to access the first row even if you have no row at all in this array. Chek rowRate.Length to see if you found a row.
If rowRate.Length=1 Then etc...
Or as here it seems you'll find one row or none, you could deifne this value as a primary key and use