|
-
March 14th, 2009, 07:51 PM
#1
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
-
March 15th, 2009, 07:32 AM
#2
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
-
March 15th, 2009, 07:10 PM
#3
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
-
March 15th, 2009, 07:29 PM
#4
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.
-
March 15th, 2009, 07:42 PM
#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
-
March 15th, 2009, 10:42 PM
#6
-
March 16th, 2009, 01:29 AM
#7
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.
-
March 16th, 2009, 06:11 PM
#8
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
-
March 16th, 2009, 09:13 PM
#9
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|