Hey. I am trying to return a value from a column if it is the highest value in all rows.
However, if there is 2 or more rows with the same max value, I use a for loop to extract that rows Name (Name is a column)
But.. iMaxBreaks is always 0. So it only returns rows that the column break_count's value is 0. But 0 is not the highest value
To recap iMaxBreaks is always set to 0, not the max value
Any ideas what I am doing wrong?
However, if there is 2 or more rows with the same max value, I use a for loop to extract that rows Name (Name is a column)
But.. iMaxBreaks is always 0. So it only returns rows that the column break_count's value is 0. But 0 is not the highest value
VB.NET:
Private Sub frmViewStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' this sub will handle the connection to the database
' these variables will hold the returned values from the database queries
Dim iMaxBreaks As Integer
Dim i As Integer = 0 ' used to control the for loop
Me.OleDbConnection2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\mydb.mdb"
Me.OleDbConnection2.Open() ' open connection to my database
Try
Me.OleDbSelectCommand1.CommandText = "SELECT MAX(break_count) FROM users" ' select the row where break_count is the highest
Me.OleDbSelectCommand1.Connection = OleDbConnection2
OleDbDataAdapter1.Fill(dst, "Users")
DataGrid1.DataSource = dst.Tables("Users")
SQLreturn = dst.Tables("Users").Rows.Count ' get number of rows
MessageBox.Show(SQLreturn)
If Not dst.Tables("Users").Rows(i).Item("Break_Count") Is DBNull.Value Then
iMaxBreaks &= dst.Tables("Users").Rows(i).Item("Break_Count")
End If
MessageBox.Show(iMaxBreaks)
Me.OleDbSelectCommand1.CommandText = "SELECT * FROM users WHERE break_count = " & iMaxBreaks & ""
Me.OleDbSelectCommand1.Connection = OleDbConnection2
dst.Clear()
OleDbDataAdapter1.Fill(dst, "Users")
SQLreturn = dst.Tables("Users").Rows.Count
MessageBox.Show(SQLreturn)
For i = 0 To SQLreturn - 1
If Not dst.Tables("Users").Rows(i).Item("Break_Count") Is DBNull.Value Then
lblMostBreaks.Text &= dst.Tables("Users").Rows(i).Item("Name")
lblMostBreaks.Text += ", "
End If
Next
MessageBox.Show(SQLreturn - 1)
Catch ex As Exception
MessageBox.Show(ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Me.OleDbConnection2.Close()
End Sub
To recap iMaxBreaks is always set to 0, not the max value
Any ideas what I am doing wrong?