Avatar of Brian
BrianFlag for United States of America asked on

ASP.NET: System.Net Ping Help!

Hi,

I need help with finishing what i started below. I'm very new to this namespace and found a few tutorials online, but do not know how to handle if an hostname / IP is un pingable.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""
            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)
            txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
            txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
            txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
            txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

        Catch err As Exception

            lblStatus.Text = err.Message
        End Try
    End Sub
ASP.NET.NET ProgrammingProgramming Languages-Other

Avatar of undefined
Last Comment
prairiedog

8/22/2022 - Mon
raterus

ASKER
Brian

raterus,

Yes, your right. I tried implenting that last night and I kept getting the following error message below. I'm not sure If i have the code in the right place or not. I'm adding the code below withing the Try should I have this somewhere else. Also, it doesnt matter what I add after If pingreply.Status =. I tried them all last night and got the same error.

If pingreply.Status = IPStatus.BadDestination Then
                label4.Text = "Host Unreachable"
End If

Error Message:
Object reference not set to an instance of an object.
raterus

Learning with you here, Your pingreply object is empty, you probably can do something like this as a failsafe
Dim pingreply As PingReply = ping.Send(txtHost.Text)
 
if Not pingreply is Nothing Then
  txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
  txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
  txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
  txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
else
  txtPing.Text = "Not working"
end if

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
Brian

:(
I keep getting that pesky error.

Object reference not set to an instance of an object.
raterus

Post your troublesome code right now, the whole mess of the ping stuff.
ASKER
Brian

below is my codebehind:

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text

Partial Class pingutility
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""

            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)
                txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
                txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
                txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
                txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

        Catch err As Exception
            lblStatus.Text = err.Message
        End Try
    End Sub
End Class
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
raterus

Of course you're still getting the error, you didn't implement the last code example I shared :-p
ASKER
Brian

raterus,

I did implement what you sent me but it still produced the same error. The code above was my original code. Below is the code that you provided.

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text

Partial Class pingutility
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub PingUtility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PingUtility.Click

        Try
            lblStatus.Text = ""

            Dim ping As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(txtHost.Text)

            If Not pingreply Is Nothing Then
                txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
                txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
                txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
                txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
            Else
                txtPing.Text = "Not working"
            End If

            'Dim pingreply As PingReply = ping.Send(txtHost.Text)

            'If Not pingreply Is Nothing Then
            '    txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
            '    txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime & Constants.vbCr
            '    txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl & Constants.vbCr
            '    txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr
            'Else
            '    txtPing.Text = "Not working"
            'End If
     
        Catch err As Exception

            lblStatus.Text = err.Message
        End Try
    End Sub
End Class
prairiedog

If txtHost.Text is empty, then an ArgumentNullException will be raised.
Your help has saved me hundreds of hours of internet surfing.
fblack61
prairiedog

I meant "thrown", not "raised". Sorry.
ASKER
Brian

prairedog,

What happens is that when i enter a host name for example www.microsoft.com and hit my Ping button then that is when I get the error message because they must have ping disabled. If I ping www.asp.net then the info is displayed in txtPing.Text. It only works when the remote computer is pingable if not then I get the error message.
ASKER CERTIFIED SOLUTION
prairiedog

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Brian

Prairiedog,

Thank you very much. That worked out just as i needed. Thank you very much for your help.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
prairiedog

Glad it worked out. But honestly, I can't take the full credits for the answer...I believe raterus's comments are very helpful.
raterus

You finally stalked my answers enough to steal one, <raspberry> :-)
prairiedog

Sorry, raterus. I didn't mean to. I waited for about one hour and didn't see any comments from you, so I thouhgt you might have moved on to another question. I really hope asp_net2 can split the points.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
raterus

Believe me, no offense taken, points really don't matter anyway when you're in the range I'm in.  This isn't the first time something like this has happened.  You learn to move on with life.
prairiedog

>>You learn to move on with life.
That explains why you are Genius and I am only Master. Still learning. :-)
Thanks for your commnet. It really made me feel a lot better.