Check or Test user Internet Connectivity using Asp.Net C# VB.Net

In some cases specially for AJAX projects C# VB.Net developers need to check user internet connection before attempting to connect to remote server. Also another important usage is for dashboard where after certain time interval you may need to know does the user connected with the network or not. Here in this article i will describe how one can check or test user internet connectivity using Asp.Net C# VB.Net server side code.

The strategy is to start download a page from server. If you can download means you are connected to the internet otherwise not. Here i am using Google page but you can use any simple page for better performance. Look at the below code:

Check user internet connectivity using Asp.Net Csharp

To Do the Example Prepare Some UI:

    <asp:Label runat="server" ID="lblCaption" Font-Bold="true" ForeColor="DarkBlue">To test your connection click below</asp:Label><br />
    <asp:Button runat="server" ID="cmdCHeck" Text="Check Connectivity" OnClick="cmdCHeck_Click" />

Now Check or Test user Internet Connectivity under cmdCHeck Button:
C# Code:

    protected void cmdCHeck_Click(object sender, EventArgs e)
        WebClient workstation = new WebClient();
        byte[] data = null;
            data = workstation.DownloadData("");
        catch (Exception ex)
        if (data != null && data.Length > 0)
            lblCaption.Text = "You are connected.";
            lblCaption.Text = "You are not connected.";

Note: Don’t forget to add “System.Net” namespace.

VB.Net Code:

    Protected Sub cmdCHeck_Click(sender As Object, e As EventArgs)
        Dim workstation As New WebClient()
        Dim data As Byte() = Nothing
            data = workstation.DownloadData("")
        Catch ex As Exception
        End Try
        If data IsNot Nothing AndAlso data.Length > 0 Then
            lblCaption.Text = "You are connected."
            lblCaption.Text = "You are not connected."
        End If
    End Sub

Note: Don’t forget to add “System.Net” namespace.

Hope now you can Check/Test whether user connected to the internet or not.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code