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)
    {
    }
}

Rishikesh Kumar Singh

I am a .Net developer who is very passionate about work. Have 2+  years hands on  experience in various .Net technologies. Presently i am working as System Analyst in Ebix India .

https://www.c-sharpcorner.com/members/rishikesh-kumar-singh
View All Comments