Support Forums

Full Version: [TuToRiAl] How to Check if theres any Internet Connection [TuToRiAl]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hey guys. I have not posted a tutorial in weeks. So I thought this thread would be very helpful; A lot of VB applications I make need an active internet connection in order to work, so we will use a function to detect whether theres an active internet connection at the moment or not. This can help prevent fatal errors while the application requires
internet when trying to run something when there is no internet connection.


Heres the code, first plug in the Function anywhere between 2 classes as in "Form1_Load and CheckBox1_CheckState."

[Image: 074efb6a7a57a844513dc3ae25cfc8da.png]

Function We Are Using:

Code:
Public Function IsConnectionAvailibe() As Boolean
        Dim objURL As New System.Uri("http://192.168.1.1/")

        Dim ObjWebRequest As System.Net.WebRequest
        ObjWebRequest = System.Net.WebRequest.Create(objURL)
        Dim objResp As System.Net.WebResponse

        Try
            objResp = ObjWebRequest.GetResponse
            objResp.Close()
            ObjWebRequest = Nothing
            Return True

        Catch ex As Exception
            objResp = Nothing
            ObjWebRequest = Nothing
            Return False

        End Try
    End Function

Now to manipulate the code a little bit:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsConnectionAvailibe() = True Then 'If there was internet on at the moment the application opened, add statements to enable the application for internet usage.
            Label1.Visible = False
            TextBox2.Enabled = True
            TextBox1.Enabled = True
            Button1.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = True
        ElseIf IsConnectionAvailibe() = False Then 'If there wasn't internet when the application opened, add statements to disable internet using items to prevent errors.
            Label1.Visible = True
            TextBox2.Enabled = False
            TextBox1.Enabled = False
            Button1.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
        End If
    End Sub
End Class

Now obviously you may change it from Form1_Load to a BackroundWorker or Timer. Thats just me. Enjoy! Post replies if you have questions!
Great code.

Personally, I'd just ping a network using My.Computer.Network.Ping, like google for example, and check the return value. A lot simpler and less code. Regardless, yours is great.
Really good code but it could be shorter.
Thanks for the tutorial Smile
And great code!
Hell of a lot of code for such a simple task.

(01-08-2011, 05:57 PM)Fragma Wrote: [ -> ]Hell of a lot of code for such a simple task.

Excuse me for living! lol

This is one of the most efficient ways to verify a solid internet connection.
thanks for the tutorial.
great job.
Blackhat
~ven0m
You should Never use try/catch to work out if something like the internet connection is available, always handle the return value!
(01-09-2011, 06:48 PM)Modest Wrote: [ -> ]You should Never use try/catch to work out if something like the internet connection is available, always handle the return value!

...No thanks, i'd stick to my piece of code thank you. And there are NO returns to be handled that are displayed, in my awesome code. We use the try, catch ex as exception, and end try. It is my way to go, it is the safest way to go. You only have 5 posts non the less, a true Learner. See ya later alligator.
This is alot of code for a simple task ^^ but good job on this bro.
Pages: 1 2 3