Solved

VB.net test server connectivity using ping before running code

Posted on 2009-05-18
12
708 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now