Label wont update

FuZion

Well-known member
Joined
Jan 28, 2007
Messages
47
Programming Experience
Beginner
Hello,

I'm writing this VB.net script and I'm having a bit of trouble. Here is the code...

VB.NET:
Public Sub updateMetar()
        Dim metar As String
        Dim winds As String
        Dim runways As String
        Dim windSpd As Integer
        Dim dir As Integer


        mysql.ConnectionString = "xxxxxxx"

        mysql.Open()

        Dim mySelectQuery As String = "SELECT * FROM `weather` WHERE icao='" & airport & "'"
        Dim myCommand As New MySqlCommand(mySelectQuery, mysql)
        Dim myReader As MySqlDataReader
        myReader = myCommand.ExecuteReader()
        myReader.Read()

        If myReader.HasRows Then
            metar = myReader.GetString(11) 'metar column
            winds = myReader.GetString(4) 'winds column

            windSpd = Convert.ToInt32(Mid(winds, 4, 2))
            dir = Convert.ToInt32(Mid(winds, 1, 3))

            runways = findRunways(dir, windSpd)
            lblMetarText.Text = metar
            lblRunwayText.Text = runways

        Else
            lblMetarText.Text = "Airport Not Found"
            lblRunwayText.Text = "Airport Not Found"
        End If

        mysql.Close()
    End Sub

Basically what happens is a call this from main.vb, my main form. The first time it's called the labels update correctly. However, the form is also created when it is first called. If the function is called again without the form being closed, redeclared, and opened, the function still downloads the correct data, but the labels do not update. Any idea why this is?

Thanks alot!

FuZion
 
not sure if this will help in any way, but instead of using HasRows, use the loop

VB.NET:
While myreader.Read()
'your code here
End While

the code looks ok though, maybe im just not looking hard enough. try using the while loop and let me know what happens

adam
 
couple of other comments on the code - dont use "magic numbers"

metar = myReader.GetString(11) 'metar column
winds = myReader.GetString(4) 'winds column

in years to come, youll forget what 11 and 4 mean. Additionally, if the database structure changes, the code breaks - endeavour to always use names rather than magic numbers when coding..


using string concatenation to build sql strings is not safe, look into parameterized queries...
 
Well I tried switching to a While Loop, but that still doesn't help. It works when the form is first opened, but if I call the function again it still will not update the labels. I'm stumped...

Edit: Just thinking... I'm calling this form from main.vb, not the form which contains this function. Could that be the problem?
 
Alright that did the trick, thanks a lot. If you try to run the program to see if it works, it wont.. I use a mysql connection for a few things and I've removed the connection info for security. Let me know if you need it and I'll get you the info.

Thanks again!

FuZion
 

Attachments

  • CCARS.zip
    1 MB · Views: 45
ok mate, i was having a look at your code, and i identified what might be causing a problem. i actually cant test this myself because i dont mysql installed on this computer (it's work's computer).

this sub...
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] parseAppCommand()
[/SIZE][SIZE=2][COLOR=#008000]'Make sure user entered text
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] txtAppCommand.Text [/SIZE][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'See if text is "dot" [.] command
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Mid(txtAppCommand.Text, 1, 1) = [/SIZE][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Charts
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Mid(txtAppCommand.Text, 1, 7) = [/SIZE][SIZE=2][COLOR=#800000]".charts"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] airport [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Mid(txtAppCommand.Text, 9, 12)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] chartFormOpen = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] charts [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] charts
charts.airport = airport
charts.updateCharts()
charts.Show()
chartFormOpen = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]charts.airport = airport
charts.updateCharts()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Metar/Runway Calc
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Mid(txtAppCommand.Text, 1, 6) = [/SIZE][SIZE=2][COLOR=#800000]".metar"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] airport [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Mid(txtAppCommand.Text, 8, 11)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] metarFormOpen = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] metar [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] metar
metar.airport = airport
metar.updateMetar()
metar.Show()
metarFormOpen = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]metar.airport = airport
metar.updateMetar()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]txtAppCommand.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

i have a feeling that one of your if statements isnt executing as its supposed to, and that it is going to the 'else' part...
this part specifically

VB.NET:
[/COLOR][/COLOR][/SIZE]
[COLOR=black]If[SIZE=2] metarFormOpen = [/SIZE][SIZE=2]False[/SIZE][SIZE=2] [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[SIZE=2][COLOR=#0000ff][COLOR=black][SIZE=2]    Dim[/SIZE][SIZE=2] metar [/SIZE][SIZE=2]As[/SIZE][SIZE=2] [/SIZE][SIZE=2]New[/SIZE][/COLOR][SIZE=2][COLOR=black] metar[/COLOR]
[COLOR=black]    metar.airport = airport[/COLOR]
[COLOR=black]    metar.updateMetar()[/COLOR]
[COLOR=black]    metar.Show()[/COLOR]
[COLOR=black]    metarFormOpen = [/COLOR][/SIZE][SIZE=2][COLOR=black]True[/COLOR]
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=black]Else[/COLOR]
[/SIZE][SIZE=2][COLOR=black]    metar.airport = airport[/COLOR]
[COLOR=black]    metar.updateMetar()[/COLOR]
[/SIZE][COLOR=black][SIZE=2]End[/SIZE][SIZE=2] [/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=black]

another thing i thought of, is that the airport variable isnt being set properly somewhere, and its missing an important statement, or your Mid statements are missing their intended points. you should try running through the entire function, from start to finish, step by step with a breakpoint to make sure EVERYTHING is being set or changed as it needs to.

what do you think?

regards
adam

 
From what I've learned about VB.NET so far that all sounds possible to me. I'll have to check that stuff out tomorrow. The only question I have is why would it work when I first call the function, but not when I call it a second time. Because that's what happens, when the function is first called it opens a new form, metar.vb and everything updates properly. But after it is called again with the same metar.vb still open, the labels don't update. But if I close the newly opened form and then call the function which opens up a new, identical form everything works.

Actually, now that I think about it I believe that the code is functioning properly because I ran though a few tests to make sure all the data was being formatted correctly. But I'm going to check everything again just to be sure. I'll let you know how it goes.

Thanks again for the help!
 
no probs mate. but trust me, sometimes i run test after test after test to ensure a program is working stably, and then when you change the smallest thing, that screws up something somewhere else. i have a strong feeling that something isnt being updated properly within the if statements.

definitely let me know what happens mate,

have a good one
adam
 
im not sure the application.doevents would be wise, because it might trigger off an event that he doesnt want to happen at that time.

just a thought

adam
 
Last edited:
What has DoEvents got to do with anything? There are no long running ops here...


Further points of note... we dont use Mid() any more.. Use IndexOf, Contains, Substring, StartsWith ...

If command.StartsWith(".charts") Then...

Dont declare an object to have the same name as its type - you have to use [] brackets to refer to static members, which clutter the code. Typically type names refer to generic notions, which doesnt give your code much meaning:

Dim color as New Color(255,240,240) <- bad!
Dim lightPink as New Colour(255,240,240) <- much better!

Dim metar as metar <- Bad! Use Capital names for types!
 
Alright guys, thanks a lot for all the tips. I haven't figured out what's wrong with this yet, but I'm going to keep at it. I'm also going to take the suggestions and apply them to the project.

I'll let you know if I get this figured out.

FuZion
 
Back
Top