Solved

VB.net test server connectivity using ping before running code

Posted on 2009-05-18
12
742 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free 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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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