Wednesday 17 October 2012

Checking Internet Connection in the Client Machine/System

Checking Internet Connection in the Client Machine/System

 

Today I will share a technique to check the Internet Connection in the Client Machine. Using the availability of the connection you can run a particular code according to your requirement..

Here is the associated code snippet to achieve this.

Design Part(.aspx)


<div> <asp:Label ID="Label1" runat="server" BackColor="#FFE0C0" Font-Bold="True" Font-Names="Script MT Bold" Font-Strikeout="False" Font-Underline="False" ForeColor="Fuchsia" Height="30px" Width="200px"></asp:Label><br />

 <br /> <asp:Button ID="Button1" runat="server" BackColor="#FFFFC0" ForeColor="Red" OnClick="Button1_Click" Text="Check" /></div>


Code-Behind(.aspx.cs)


using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Net;

public partial class Default2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {  

    }

    public static bool isConnected()

    {

        try

        {

            string myAddress = "www.google.com";

            IPAddress[] addresslist = Dns.GetHostAddresses(myAddress);

            if (addresslist[0].ToString().Length > 6)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        catch

        {

            return false;

        }

    }

    bool Status=isConnected();

    protected void Button1_Click(object sender, EventArgs e)

    {

        if(Status==true)

        {

         // Write Your Specific code to execute when the user is connected

        Label1.Text="I am Connected";

        }

        else

        {

        //Do This when user is not connected.

        Label1.Text="No Connection available";

        }

    }

}

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top