Asp.net How to check Internet Connection’s availability using C# Code?

To check internet connection is available or not using C# Code use below code

		WebClient client = new WebClient();	
        byte[] datasize = null;
        try
        {
            datasize = client.DownloadData("http://www.google.com");
        }
        catch (Exception ex)
        {
        }
        if (datasize != null && datasize.Length > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Internet Connection Not Available.');", true);
        }
        else
        { 
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('.Internet Connection Not Available.');", true);
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s