Solved

VB.net test server connectivity using ping before running code

Posted on 2009-05-18
12
736 Views
Last Modified: 2013-11-26
I am tring to check to see if a server is up by using ping.  If i get a response run my code.  Then loop back.  It is pulling an ip address from a text file.  It looks like it works on the first server then fails.  Im not sure if it is not holding hte information from the first ping.  Any help would be great.
Private Sub btnDeleteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteAll.Click
        Dim con As New SqlClient.SqlConnection
        Dim da As New OleDb.OleDbDataAdapter
        Dim ds As New DataSet
        Dim sql As SqlClient.SqlCommand
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim Server As String
        Dim Firstname As String = txtFirstName.Text
        Dim Surname As String = txtSurName.Text
        Dim UserInit As String = txtUserInit.Text
        Dim Result As Integer = "0"
        Dim ResultText As String = ""
        Dim fileReader As New System.IO.StreamReader("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim fileContents() As String = fileReader.ReadToEnd.Split(ControlChars.Cr)
        Dim lines As String() = IO.File.ReadAllLines("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim startnum As Integer
        Dim endnum As Integer
        
 
        startnum = 0
        endnum = lines.Length - 1
        For i = startnum To endnum
            ProgressBar1.PerformStep()
            Server = fileContents(i)
            
 
            If My.Computer.Network.Ping(Server, 1000) Then
 
                Try
                    con = New SqlClient.SqlConnection("Data Source =" & Server & ";initial catalog = Windman;User ID=user;Password=pass")
                    con.Open()
 
                    sql = New SqlClient.SqlCommand("WMS_SP_UTL_DeleteUser", con)
 
                    With sql
 
 
                        .CommandType = CommandType.StoredProcedure
                        .CommandTimeout = 0
                        '//add some parameters
                        .Parameters.Add(New SqlClient.SqlParameter("@UserInitials", UserInit))
                        .Parameters.Add(New SqlClient.SqlParameter("@Result", 0))
                        .Parameters.Add(New SqlClient.SqlParameter("@Resulttext", ""))
 
                        Try
                            .ExecuteNonQuery()
                            'MsgBox(UserInit & " deleted from " & Server)
                        Catch excA As SqlClient.SqlException
                            MsgBox(excA.Message)
                        End Try
                        con.Close()
 
                    End With
                Catch ex As Exception
                    MsgBox("Could not connect to " & Server)
                End Try
            Else
                MsgBox("Could not connect to " & Server)
            End If
 
        Next i
        ProgressBar1.Value = 100
        MsgBox("all servers updated")
        ProgressBar1.Value = 0
        MsgBox(UserInit & " Has been deleted")
 
 
    End Sub

Open in new window

0
Comment
Question by:NCSA SCADA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
12 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24413480
which bit fails? are you getting an error message or is the Ping just returning false after the 1st call?
0
 

Author Comment

by:NCSA SCADA
ID: 24413712
I get the following error .... An exception occurred during a Ping request.

I manually ping the second server in the list without problem..... If I add a message box displaying the value of the variable Server ... it shows the correct ip.....
0
 

Author Comment

by:NCSA SCADA
ID: 24413824
A couple more things..... I swaped the first and second ip's in the list.  The new first one works and it fails on the second .... I created some test code to make it easier including a couple of message boxes
  Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim Server As String
        Dim fileReader As New System.IO.StreamReader("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim fileContents() As String = fileReader.ReadToEnd.Split(ControlChars.Cr)
        Dim lines As String() = IO.File.ReadAllLines("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim startnum As Integer
        Dim endnum As Integer
 
 
        startnum = 0
        endnum = lines.Length - 1
        For i = startnum To endnum
            ProgressBar1.PerformStep()
            Server = fileContents(i)
            MsgBox(Server)
 
            If My.Computer.Network.Ping(Server, 1000) Then
                MsgBox(Server & " Up")
            Else
                MsgBox("Ping request timed out.")
 
            End If
 
        Next i
    End Sub
End Class

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:NCSA SCADA
ID: 24414680
Any thoughts experts?
0
 

Author Comment

by:NCSA SCADA
ID: 24414996
I made another change for error checking .... I get the following error .... An exception occurred during a Ping request. after every server except for the first one in the list... thanks
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim Server As String
        Dim fileReader As New System.IO.StreamReader("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim fileContents() As String = fileReader.ReadToEnd.Split(ControlChars.Cr)
        Dim lines As String() = IO.File.ReadAllLines("C:\Program Files\VOBUserMan\ServerList.txt")
        Dim startnum As Integer
        Dim endnum As Integer
        startnum = 0
        endnum = lines.Length - 1
        For i = startnum To endnum
            ProgressBar1.PerformStep()
            Server = fileContents(i)
            MsgBox(Server)
 
            'If My.Computer.Network.Ping(Server, 1000) Then
            Try
                If My.Computer.Network.Ping(Server) Then
                    MsgBox(Server & " Up")
                Else
                    MsgBox("Ping request timed out.")
 
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            
 
        Next i
    End Sub

Open in new window

0
 

Author Comment

by:NCSA SCADA
ID: 24416132
any body out there
0
 

Author Comment

by:NCSA SCADA
ID: 24418113
Hi experts, can i get some help with this?
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24420638
I just tried this and it worked ok. Are the servers external or internal?
        Dim ips As String = "192.168.0.104,62.233.107.154,192.168.0.111"
        Dim ipArray() As String = ips.Split(",")
 
        For Each ip As String In ipArray
 
            If (My.Computer.Network.Ping(ip)) Then
                MessageBox.Show("IP: " + ip + " ping ok")
            Else
                MessageBox.Show("IP: " + ip + " ping bad")
            End If
 
        Next

Open in new window

0
 

Author Comment

by:NCSA SCADA
ID: 24422171
They are remote, but I have no problem pinging them manually and the ip address that is first in my text file works 100% of the time. I can rearange the order and the first one will work. Any chance you can try it with a text file
0
 

Author Comment

by:NCSA SCADA
ID: 24422198
one more thing , i am using the text file because the list of ip's changes often and I do not want to have to change the code.
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24422837
I have found the problem - I got excatly the same bug as you so I compared the difference in my original code to reading it from a file.
When you split it you are doing it on a vbCr - this will leave other hidden characters in the string which is messing up the ping.
I got it to work by changing this line and stripping out the line feeds & carrage returns. After that all the IP's from the file worked ok.

Server = fileContents(i)
 
TO
 
Server = fileContents(i).Replace(vbLf, String.Empty).Replace(vbCr, String.Empty)

Open in new window

0
 

Author Closing Comment

by:NCSA SCADA
ID: 31582644
That was it.  Thanks for your hep with this.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Nested forach loop to linq 3 41
vb.net application has warrnings about VB6 calls. 2 42
vb.net dbnull syntax 1 48
PowerShell:  foreach where object notmatch? 17 71
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question