This is a pre-assignment for a class I'm in. Supposed to be pretty simple and act as a warm-up, but I can't get it working. Basically the code is bringing in a test database and performing a calculation. In this case I'm trying to find the highest average batting average in a set of baseball players.
So my end result should be the name of the player with the highest batting average, or a few players if they are tied for the highest average.
Here is the code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Baseball.accdb"
Dim sqlStr As String = "SELECT * FROM Players"
Dim dataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
Dim average, pastAverage, highestAverage As Double
For i As Integer = 0 To dt.Rows.Count - 1
average = CDbl(dt.Rows(i)("hits/atBats"))
If average > pastAverage Then
highestAverage = average
End If
pastAverage = average
Next
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("hits/atBats") = highestAverage Then
lstBoxHighest.Items.Add(dt.Rows(i)("name"))
End If
Next
End Sub
End Class
The debugger won't go past the "average = Cdbl(dt.Rows(i)("hits/atBats"))" line in the first For Loop. Can I not do calculations like that in the loop? I am sure the column titles (hits and atBats are correct)
The database looks like this in case you were wondering:
name Team atBats hits Derek Jeter New York Yankees 511 158 Joe Mauer Minnesota Twins 545 174 etc...
Thanks!