5 Replies - 329 Views - Last Post: 13 January 2018 - 04:38 AM Rate Topic: -----

#1 Damein   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 29-March 14

If Else Statement.. not working?

Posted 12 January 2018 - 05:42 PM

So I have a SQL DB I'm running a SELECT statement and then grabbing the lowest and highest values.

        Dim TempLow As String
        Dim TempHigh As String

        TempLow = 100.0
        TempHigh = 0.00
        Statement = "SELECT Temperature, Humidity, Pressure, timeStamp FROM readings WHERE timeStamp LIKE @Time"
        Using con As New MySqlConnection(DBConnection)
            Using cmd As New MySqlCommand(Statement)
                Using sda As New MySqlDataAdapter()
                    cmd.Parameters.AddWithValue("@Time", "%" & todaysdate & "%")
                    cmd.Connection = con
                    sda.SelectCommand = cmd

                    Dim ds As New DataSet()
                    sda.Fill(ds)

                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MessageBox.Show(ds.Tables(0).Rows(i).Item(0).ToString)
                        If TempLow > ds.Tables(0).Rows(i).Item(0) Then
                            TempLow = ds.Tables(0).Rows(i).Item(0).ToString
                        End If
                        If ds.Tables(0).Rows(i).Item(0) > TempHigh Then
                            TempHigh = ds.Tables(0).Rows(i).Item(0).ToString
                        End If
                    Next

                    MessageBox.Show(TempLow & " - " & TempHigh)



                End Using
            End Using
        End Using



The MessageBox showing each iteration shows a variance of 57 and 56.

The final MessageBox showing the TempLow and TempHigh shows: 100 - 57

So somehow it doesn't catch the TempLow ? Not sure why.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: If Else Statement.. not working?

#2 Damein   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 29-March 14

Re: If Else Statement.. not working?

Posted 12 January 2018 - 06:01 PM

Buuuuuuuuuuuut... if I add this it works fine.. how is that?

(Removed the initial assignment of TempLow to a high value and left it empty)

                        If TempLow = "" Then
                            TempLow = ds.Tables(0).Rows(i).Item(0).ToString
                        End If



Dim TempLow As String
        Dim TempHigh As String

        TempHigh = 0.00
        Statement = "SELECT Temperature, Humidity, Pressure, timeStamp FROM readings WHERE timeStamp LIKE @Time"
        Using con As New MySqlConnection(DBConnection)
            Using cmd As New MySqlCommand(Statement)
                Using sda As New MySqlDataAdapter()
                    cmd.Parameters.AddWithValue("@Time", "%" & todaysdate & "%")
                    cmd.Connection = con
                    sda.SelectCommand = cmd

                    Dim ds As New DataSet()
                    sda.Fill(ds)

                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        If TempLow = "" Then
                            TempLow = ds.Tables(0).Rows(i).Item(0).ToString
                        End If

                        If TempLow > ds.Tables(0).Rows(i).Item(0) Then
                            TempLow = ds.Tables(0).Rows(i).Item(0).ToString
                        End If
                        If ds.Tables(0).Rows(i).Item(0) > TempHigh Then
                            TempHigh = ds.Tables(0).Rows(i).Item(0).ToString
                        End If
                    Next

                    MessageBox.Show(TempLow & " - " & TempHigh)



                End Using
            End Using
        End Using



Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: If Else Statement.. not working?

Posted 12 January 2018 - 06:12 PM

Any particular reaaon you are using strings instead of a numeric data type?
Was This Post Helpful? 0
  • +
  • -

#4 Damein   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 29-March 14

Re: If Else Statement.. not working?

Posted 12 January 2018 - 06:28 PM

More for just the rough draft. A string can be anything so need to worry about a conversion error or anything. In the final I plan to assign as Doubles, integers ect. but just saw no reason for it now :)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: If Else Statement.. not working?

Posted 13 January 2018 - 02:42 AM

Numbers should be numbers, especially for comparison. "100" < "2".

Put Option Strict at the top of your code to enforce strict data typing.
Was This Post Helpful? 1
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,808
  • Joined: 01-February 13

Re: If Else Statement.. not working?

Posted 13 January 2018 - 04:38 AM

Using the Less Than (<) and Greater Than (>) comparison operators does not work the same for String types as it does for Numeric types. With String types, as andrewsw was hinting at, would consider "100" as less than "20". With Strings, these operators compare the strings in a String Sorting order, not a Numeric Value order which you want.

What i mean by a Sorting order is, "A" in "Amy" comes before "B" in "Bob" when sorting strings in an alphabetical order. That is the same as "1" in "100" comes before "2" in "20" in a string sorting order.

See the (Comparing Strings) section in the link below.
Comparison Operators in Visual Basic
Try this and see what it does...
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'compare these two strings
        Dim s1 As String = "100000"
        Dim s2 As String = "2"

        If s1 < s2 Then
            MessageBox.Show(s1 & " is less than " & s2, "Comparing Strings...")
        ElseIf s1 = s2 Then
            MessageBox.Show(s1 & " is equal to " & s2, "Comparing Strings...")
        Else
            MessageBox.Show(s1 & " is greater than " & s2, "Comparing Strings...")
        End If


        'convert the two strings to Integer values and try comparing them
        Dim n1 As Integer = 0
        Dim n2 As Integer = 0
        Integer.TryParse(s1, n1)
        Integer.TryParse(s2, n2)

        If n1 < n2 Then
            MessageBox.Show(n1.ToString & " is less than " & n2.ToString, "Comparing Integers...")
        ElseIf n1 = n2 Then
            MessageBox.Show(n1.ToString & " is equal to " & n2.ToString, "Comparing Integers...")
        Else
            MessageBox.Show(n1.ToString & " is greater than " & n2.ToString, "Comparing Integers...")
        End If
    End Sub
End Class


Now you will see what is happening and will understand that you can not compare Strings as if they where Numeric types of data without converting them to the correct Numeric type first.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1