CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: database loop

  1. #1
    Join Date
    Mar 2009
    Posts
    5

    database loop

    Hello All

    Average Vb 2005 user here!

    I currently have the below code to ping the first record in my database.

    I'd like to set it up so that if an IP in the Database pings, it moves on to the next record e.g NetMonListDataSet1.Addresses.Rows(2).Item(0).ToString. if it doesn't receive a response it "sendsmymail()".

    I know i need a For Each loop somewhere maybe? But i can't seem to fit it in.

    Any help would be most welcome.


    Code:
    Dim myProcess As New Process()
            myProcess.StartInfo.UseShellExecute = False
            myProcess.StartInfo.RedirectStandardOutput = True
            Try
                myProcess.StartInfo.FileName = ("ping")
                myProcess.StartInfo.Arguments = NetMonListDataSet1.Addresses.Rows(1).Item(0).ToString
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.Start()
                TextBox1.Text = _
                   Replace(myProcess.StandardOutput.ReadToEnd(), _
                   Chr(13) & Chr(13), Chr(13))
                myProcess.WaitForExit()
    
                If TextBox1.Text.Contains("unreachable") Or TextBox1.Text.Contains("Timed") Then
                    sendmymail()
                Else
                End If
    
            Catch ex As System.ComponentModel.Win32Exception
                MsgBox((ex.Message + ". Error Detected."))
            End Try
    
        End Sub
    Thanks

    Paul

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: database loop

    Code:
    For I as Integer=1 to NetMonListDataSet1.Addresses.Count
           Dim myProcess As New Process()
            myProcess.StartInfo.UseShellExecute = False
            myProcess.StartInfo.RedirectStandardOutput = True
            Try
                myProcess.StartInfo.FileName = ("ping")
                myProcess.StartInfo.Arguments = NetMonListDataSet1.Addresses.Rows(I).Item(0).ToString
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.Start()
                TextBox1.Text = _
                   Replace(myProcess.StandardOutput.ReadToEnd(), _
                   Chr(13) & Chr(13), Chr(13))
                myProcess.WaitForExit()
    
                If TextBox1.Text.Contains("unreachable") Or TextBox1.Text.Contains("Timed") Then
                    sendmymail()
                Else
                End If
    
            Catch ex As System.ComponentModel.Win32Exception
                MsgBox((ex.Message + ". Error Detected."))
            End Try
    
        End Sub
    Next I

  3. #3
    Join Date
    Mar 2009
    Posts
    5

    Re: database loop

    Hello,

    Thanks, that worked well.

    I have put a try catch block here
    Code:
                 Try
                    myProcess.StartInfo.Arguments = NetMonListDataSet1.Addresses.Rows(I).Item(0).ToString
                Catch
                    MessageBox.Show("No more rows")
                End Try
    As it had an error that it was reaching the end of the rows.

    Is this the best way to do it? I couldn't find dataset1.empty or anything similar?

    Thanks

    Paul

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: database loop

    Instead of

    Code:
    For I as Integer=1 to NetMonListDataSet1.Addresses.Count
    try
    Code:
    For I as Integer=0 to NetMonListDataSet1.Addresses.Count-1
    Indexing starts at 0 in many VB objects so the first item is 0 and the last item is count-1. Odds are that you are not getting the first record with your current code and trying to read one past the eof marker.
    Last edited by DataMiser; March 15th, 2009 at 07:32 PM.

  5. #5
    Join Date
    Mar 2009
    Posts
    5

    Re: database loop

    ah ha! yet another issue solved!

    thanks you again!

    Last issue I swear................

    I'd like something along the lines of

    Code:
    TextBox2.Text = ("Searching For " & NetMonListDataSet1.Addresses.Rows(I).Item(0).ToString)
    But it only appears after the last record? Anyway I can fit this in? Don't worry if you're fed up of responding to dumb questions. I should be able to work it out after some testing but you may know the answer instantly.

    Thankyou all again.

    Paul

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: database loop

    Show your code...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: database loop

    Not much to go on in the last question but if I had to guess I would say that.
    1: Most likely need a textbox2.refresh stuck in there
    2: The code may execute to fast to see anything other than the last item.

  8. #8
    Join Date
    Mar 2009
    Posts
    5

    Re: database loop

    Hello,

    Code:
    Sub Ping()
            For I As Integer = 0 To NetMonListDataSet1.Addresses.Count - 1
                Dim myProcess As New Process()
                myProcess.StartInfo.UseShellExecute = False
                myProcess.StartInfo.RedirectStandardOutput = True
                myProcess.StartInfo.FileName = ("ping")
                '     Try
                myProcess.StartInfo.Arguments = NetMonListDataSet1.Addresses.Rows(I).Item(0).ToString
                TextBox2.Text = ("Searching For " & NetMonListDataSet1.Addresses.Rows(I).Item(0).ToString)
                '     Catch
                '    MessageBox.Show("No more rows")
                '   End Try
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.Start()
                TextBox1.Text = _
                   Replace(myProcess.StandardOutput.ReadToEnd(), _
                   Chr(13) & Chr(13), Chr(13))
                myProcess.WaitForExit()
                If TextBox1.Text.Contains("unreachable") Or TextBox1.Text.Contains("timed") Then
                    sendmymail()
                Else
    
                End If
            Next I
    As stated before it is only showing the last IP address in textbox2. So all other IP's in the database are ignored and the last one appears "searching for 192.168.1.5". It does seem to execute too fast, could I put a timer event on?

    Thanks again.

    Paul

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: database loop

    It looks like it's working correctly, except that the textbox gets overwritten by each value, and it shows the last value. Why not add the items to a ListBox?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured