Checking out internet connection using ASP.NET C#

Step-1:  Add a button and a label in .aspx page.

<asp:Button runat="server" ID="Button1" Text="Check Internet Connection" onclick="Button1_lick"/>
<asp:Label runat="server" ID="Label1" />

Step-2: Now on the click event of the button write down the code in .aspx.cs page. Add a namespace

using System.Net;

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        WebClient wc = new WebClient();
        byte[] dataSize = null;
        dataSize = wc.DownloadData("http://www.google.com");
        if (dataSize!= null && dataSize.Length > 0)
            Label1.Text = " Connection is Available.";
        else
            Label1.Text = " Connection is not Available.";
    }
    catch (Exception ex)
    {
    }
}

Ebook Download
View all
Learn
View all