Search code examples
vb.netloopsfor-loopexecuteresolve

How to solve codes after for loop that do not execute in vb.net?


I have this code under my form_load

    checkUser = False
    MsgBox("test start")
    result = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ArrayList)(getJSon("https://dtitdtr.herokuapp.com/employees"))
    MsgBox("test after result before for-each")
    For Each value As Object In result
        token = JObject.Parse(value.ToString())
        id = token.SelectToken("id")
        fname = token.SelectToken("fname")
        mname = token.SelectToken("mname")
        lname = token.SelectToken("lname")
        contact = token.SelectToken("contactno")
        add = token.SelectToken("address")
        user = token.SelectToken("username")
        pass = token.SeelectToken("password")
        If user.ToString().ToUpper().Equals(GetUName()) Then
            checkUser = True
            Exit For
        Else
            checkUser = False
        End If
    Next value
    MsgBox("test after next value")
    reader.Close()
    response.Close()

and when I run the program, the first two(2) message boxes displayed and the last one, which is after the Next Value, won't display.

I don't quite get what's going on; since yerterday, when I run it, it went just fine and right now after adding codes for update info, which does not suppose to affect the form_load, the codes right after for loop won't execute. What is the problem with this?

I got something here that says "an exception is being thrown" but I don't even have a Try Catch in my code.


Solution

  • Hope that your code throws some Exception inside the For, so i suggest you to include Try.. Catch get the exception details. that helps you to detect the problem.

            Try
                For Each value As Object In result
                    token = JObject.Parse(value.ToString())
                    id = token.SelectToken("id")
                    fname = token.SelectToken("fname")
                    mname = token.SelectToken("mname")
                    lname = token.SelectToken("lname")
                    contact = token.SelectToken("contactno")
                    add = token.SelectToken("address")
                    user = token.SelectToken("username")
                    pass = token.SeelectToken("password")
                    If user.ToString().ToUpper().Equals(GetUName()) Then
                        checkUser = True
                        Exit For
                    Else
                        checkUser = False
                    End If
                Next value
                MsgBox("test after next value")
                reader.Close()
                response.Close()
            Catch ex As Exception
                MsgBox("Exception :" & ex.ToString)
            End Try