Results 1 to 5 of 5

Thread: Loop stop un-expected

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Loop stop un-expected

    My loop always stop when encounter a False return
    What I expect is to run other code block if False is return

    The loop stop whem False is return form this
    If isReg(dr(1).ToString(), dr(2).ToString().Substring(6, 5)) = True Then
    When False return, the code in Else block mus be run before going to Next item on my Reader.
    But my codes seems correct..I can't find where that error came from.
    But my suspect is this,
    Code:
    If Not sendRequest(dr(1).ToString(), generateRequest(dr(2).ToString().Substring(6, 5), dr(2).ToString().Substring(11, 7), dr(2).ToString().Substring(19, 3))) Then
                                'if false, do something before going to Next
                                
                            End If

    Code:
    Private Sub loopRequestedCode()
            Dim da As New MySqlDataAdapter()
            Dim ds As New DataSet()
            Dim dt As DataTable
            Dim dr As DataRow
            Dim conn As MySqlConnection = New MySqlConnection()
            Try
                conn.ConnectionString = clsComm.connString()
                Dim SQL As String = "SELECT * from tbl_request"
                conn.Open()
                da.SelectCommand = New MySqlCommand(SQL, conn)
                Dim myCB As New MySqlCommandBuilder(da)
                da.Fill(ds, "Request")
                dt = ds.Tables(0)
                For Each dr In dt.Rows
                    If isMatch(dr(2).ToString()) = True Then
                        If isReg(dr(1).ToString(), dr(2).ToString().Substring(6, 5)) = True Then
                            If Not sendRequest(dr(1).ToString(), generateRequest(dr(2).ToString().Substring(6, 5), dr(2).ToString().Substring(11, 7), dr(2).ToString().Substring(19, 3))) Then
                                'if false, do something before going to Next
                                
                            End If
                        Else
                            If Not sendRequest(dr(1).ToString(), notReg) Then
                                'if false, do something before going to Next
                            End If
                        End If
                    Else
                        If Not sendRequest(dr(1).ToString(), invalidReq) Then
                            'if false, do something before going to Next
                        End If
                    End If
                Next
                ds.Dispose()
                conn.Close()
            Catch ex As Exception
                'TODO: catch error here
            Finally
                conn.Close()
            End Try
        End Sub
    Code:
        Private Function isReg(ByVal sender As String, ByVal sID As String) As Boolean
            Dim MyDA As New MySqlDataAdapter()
            Dim MyDS As New DataSet()
            Dim conn As MySqlConnection
            conn = New MySqlConnection()
            conn.ConnectionString = clsComm.connString()
            Dim SQL As String = "SELECT * from tbl_tableS WHERE stID='" + sID + "'"
            conn.Open()
            MyDA.SelectCommand = New MySqlCommand(SQL, conn)
            Dim cb As New MySqlCommandBuilder(MyDA)
            MyDA.Fill(MyDS, "isReg")
            If MyDS.Tables Is Nothing Then
                Return False
            Else
    
                Dim dt As DataRow = MyDS.Tables(0).Rows(0)
                Dim fromSender As String = sender.Remove(0, 3)
                Dim fromDB As String = dt(7).ToString().Remove(0, 1)
                If (fromSender.Equals(fromDB)) = True Then
                    Return True
                Else
                    Return False
                End If
            End If
            conn.Close()
        End Function
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Loop stop un-expected

    The most likely reason that an exception is being thrown. Put some sensible code in your Catch block and you will find out whether that is true and, if it is, exactly what went wrong and where.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Loop stop un-expected

    I found it
    error came from this Function isReg()
    that function throw an error which IndexOutOfRange: There is nor Row at position 0
    Coz values forwarded was not found on database.
    Hmmm...I'm thinking what's the best approach to that issue now.
    Last edited by dr_aybyd; Aug 27th, 2012 at 11:04 PM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Loop stop un-expected

    The obvious approach would be to check whether there are rows before trying to use them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Loop stop un-expected

    Quote Originally Posted by jmcilhinney View Post
    The obvious approach would be to check whether there are rows before trying to use them.
    Yeah that's what I'm doing now..I put code block to check if HasRows
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width