Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How can we check that our internet connection is working or not.

Author: DevASP
Download Source Code : 639_IsConnected.zip

This article is about how you can check either your internet connection is working or not. This application us a timer control so that it can check the internet connection periodically weather your in......

Steps you will do.

 

Start visual studio and create a new window application.Set the following properties of the form object

 

 

Name = “Form1"

BorderStyle = “Fixed3D”

SartPosition = “CenterScreen”

Text = “Internet Connection?”

ForeColor = “Control’

BackColor = “Control’

 

 

Place a label control on the form and set its following properties.

 

Name = “lblResult’

Text = “’

Font = “Microsoft Sans Serif, 9pt, style=Bold”

ForeColor = “GreenYellow”

TextAlign = “MiddleLeft”

   

Place a picture box control on the form and set its following properties.

 

Name = “picResult’

SizeMode = “AutoSize”

 

 

Now finally place a timer control on the form and set its following properties.

 

 

Name = “imer1”

Interval = “700”

 

 

Open the code window of form and declare the following variables globally.

 

Private TimeOut As Integer

Private IsConnected As Boolean

 

 

In the load event of the form write the following code which will detect your internet connection and set the display message and image that will be shown depending on the status of the picture box.

 

 

        Cursor = Cursors.WaitCursor

        If IsConnectionAvailable() Then

        picResult.Image = Image.FromFile(Application.StartupPath & "\Yes.ico")

        

    lblResult.Text = "Internet Connection Found"

        Else

            picResult.Image = Image.FromFile(Application.StartupPath & "\No.ico")

 

            lblResult.Text = "Internet Connection Not Found"

            lblResult.ForeColor = Color.Red

        End If

        Me.TopMost = True

        Timer1.Start()

        Cursor = Cursors.Default

 

The function “IsConnectionAvailable” called in the above code returns the Boolean value depending on the status of the internet connection. If your internet connection is found this function will return the true value otherwise false. On the basis of its return value we decided the display message and image. The function is as below.

 

Public Function IsConnectionAvailable() As Boolean

      

Dim objWebReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://www.microsoft.com/")

 

        Dim objResp As System.Net.WebResponse

        Try

            objResp = objWebReq.GetResponse

            objResp.Close()

            objWebReq = Nothing

            IsConnected = True

            Return True

        Catch ex As Exception

            objResp = Nothing

            objWebReq = Nothing

            IsConnected = False

            Return False

        End Try

    End Function

 

 

Now in the tick event of the timer control write the following code. The timer object is used to check the internet connection periodically.

 

        TimeOut += 1

        If TimeOut = 9 Then

            Timer1.Stop()

        End If

        If IsConnected Then

            lblResult.ForeColor = Color.GreenYellow

        Else : lblResult.ForeColor = Color.Red

        End If

        If TimeOut Mod 2 = 0 Then

            lblResult.ForeColor = Color.Black

        End If

         

 

Note that we use two images in the application. Which image will be displayed in the picture box depends on the status of the internet connection. If the internet connection is found then the “Yes” image is displayed otherwise “No” image displayed. You can get these images from the attached application. You cal also set your own images. 

 

Finally run your application and check the status of your internet connection.

Article Comments
OW TO CHECK IF INTERNET CONNECTON IS ON

Posted on 2/24/2007 2:43:50 AM by GAGAN

this is very good and help me
thank you very much
i want informations about internet connection in csharp
can you help me?

Posted on 5/10/2007 3:53:36 AM by mina

can u please tell me how to get this logic work in ASP.NET?

Posted on 8/8/2007 11:58:07 PM by Manoj Panchal

thank you very much

Posted on 8/29/2008 7:15:14 AM by Dody

Thank u very much..

Posted on 11/11/2009 8:13:24 PM by srinivas

How to change the image?

Posted on 12/12/2009 11:18:06 AM by yaha

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How we can convert a Character to Binary and ASCII code.

Disclaimer - Privacy
© 2002-2017 DevASP.net