Wednesday, 1 May 2013

Check out internet connection using asp.net C#

Check out internet connection using asp.net C#

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

---------------------------------------------------------------
.cs
---------------------------------------------------------------
using System.Net;

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