Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code for minimum values return the maximum value and the code for maximum returns 0
VB
Private Sub BtnCalcMin_Click(sender As Object, e As EventArgs) Handles btnCalcMin.Click
        'Minimum Calculation 

        Dim strsql As String = "SELECT [Price] FROM [Booking]"
        Dim strpath As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\User\Desktop\Dummy database for login register\Flight2\Flight\test.mdb"

        Dim odaTest As New OleDb.OleDbDataAdapter(strsql, strpath)
        Dim datvalue As New DataTable

        Dim intcount As Integer

        Dim decMinimum As Decimal = 0

        odaTest.Fill(datvalue)
        odaTest.Dispose()


        For intcount = 0 To datvalue.Rows.Count - 1
            If decMinimum >= Convert.ToDecimal(datvalue.Rows(intcount)("Price")) Then
                decMinimum = Convert.ToDecimal(datvalue.Rows(intcount)("Price"))
            Else
                decMinimum = decMinimum
            End If
        Next

        Label5.Text = "RM" & decMinimum

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       'Maximum Calculation 

        Dim strsql As String = "SELECT [Price] FROM [Booking]"
        Dim strpath As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\User\Desktop\Dummy database for login register\Flight2\Flight\test.mdb"

        Dim odaTest As New OleDb.OleDbDataAdapter(strsql, strpath)
        Dim datvalue As New DataTable

        Dim intcount As Integer

        Dim decMax As Decimal = 0

        odaTest.Fill(datvalue)
        odaTest.Dispose()

        For intcount = 0 To datvalue.Rows.Count - 1
            If decMax < Convert.ToDecimal(datvalue.Rows(intcount)("Price")) Then
                decMax = Convert.ToDecimal(datvalue.Rows(intcount)("Price"))
            End If
        Next

        Label6.Text = "RM" & decMax
    End Sub


What I have tried:

I tried changing the data type from currency to numbers
Posted
Updated 15-Dec-19 6:08am
Comments
phil.o 15-Dec-19 11:56am    
This is a mathematical issue: you do not use the right comparisons for maximum/minimum values. This is the kind of problem which can be understood/solved by a simple debug session. Why don't you debug your code?

Quote:
My code for minimum values return the maximum value and the code for maximum returns 0

For Mon and Max, never start with a fixed value like 0, always start with an element of the list, usually the first.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
[no name] 15-Dec-19 12:05pm    
"For Min and Max, never start with a fixed value like 0, always start with an element of the list, usually the first." *thumbsup*

Or at least initialize it with e.g. double.Min or double.Max
On SQL Server level:
SQL
SELECT DISTINCT MIN([Price]), MAX([Price]) FROM [Booking]


On client level
VB
Dim minval = datvalue.AsEnumerable().Min(Function(x) x.Field(of Integer)("Price"))
Dim maxval = datvalue.AsEnumerable().Max(Function(x) x.Field(of Integer)("Price"))
 
Share this answer
 
Comments
Wafiqque 15-Dec-19 11:43am    
it says sequence contains no element
The variable decMinimum starts at zero, so it will never be replaced by any other value. The decMaximum also starts at zero, but we must assume there are no values in the datatable otherwise it would be replaced. As Patrice T suggests, use the debugger to see what actual values (if any) you are processing. Also remove that redundant Else clause in your code:
VB
If decMinimum >= Convert.ToDecimal(datvalue.Rows(intcount)("Price")) Then
    decMinimum = Convert.ToDecimal(datvalue.Rows(intcount)("Price"))
' Else                         these lines are redundant
'    decMinimum = decMinimum           ditto
End If
 
Share this answer
 
Comments
Richard MacCutchan 15-Dec-19 15:47pm    
1. Do not use float or double types for financial values; use integer or decimal.
2. When searching for a maximum you should initialise it to either the first value in the list or zero.
3. There is no point in recapturing the value from element zero every iteration through your loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900