Click to See Complete Forum and Search --> : database loop
bona_bone1
March 14th, 2009, 07:51 PM
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.
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
sotoasty
March 15th, 2009, 07:32 AM
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
bona_bone1
March 15th, 2009, 07:10 PM
Hello,
Thanks, that worked well.
I have put a try catch block here
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
DataMiser
March 15th, 2009, 07:29 PM
Instead of
For I as Integer=1 to NetMonListDataSet1.Addresses.Count
try
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.
bona_bone1
March 15th, 2009, 07:42 PM
ah ha! yet another issue solved!
thanks you again!
Last issue I swear................
I'd like something along the lines of
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
dglienna
March 15th, 2009, 10:42 PM
Show your code...
DataMiser
March 16th, 2009, 01:29 AM
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.
bona_bone1
March 16th, 2009, 06:11 PM
Hello,
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
dglienna
March 16th, 2009, 09:13 PM
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?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.