Solved

VB.net test server connectivity using ping before running code

Posted on 2009-05-18
12
720 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 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