[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB.net test server connectivity using ping before running code

Posted on 2009-05-18
12
Medium Priority
?
759 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 2000 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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