Thursday, February 18, 2010

Check user internet connectivity using Asp.Net C#




Please visit my new Web Site WWW.Codedisplay.com



In some cases specially for AJAX projects Asp.net 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 find user internet connectivity using asp.net C# 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:






WebClient workstation = new WebClient();
        byte[] data = null;

        try
        {
            data = workstation.DownloadData("http://www.google.com");
        }
        catch (Exception ex)
        {
        }
        
        if (data != null && data.Length > 0) 
            lblCaption.Text="You are connected.";
        else
            lblCaption.Text="You are not connected.";
To complete an example first add a page in your project. Then write the below Markup Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Internet_Connectivity.aspx.cs" Inherits="Internet_Connectivity" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>How to check user internet connection</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label runat="server" ID="lblCaption" Font-Bold="true" ForeColor="DarkBlue">To test your connection click below</asp:Label>
    <br />
    <br />
    <asp:Button runat="server" ID="cmdCHeck" Text="Check Connectivity" OnClick="cmdCHeck_Click" />
    </div>
    </form>
</body>
</html>
Under code behind the complete code is given below:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

public partial class Internet_Connectivity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdCHeck_Click(object sender, EventArgs e)
    {
        WebClient workstation = new WebClient();
        byte[] data = null;
        try
        {
            data = workstation.DownloadData("http://www.google.com");
        }
        catch (Exception ex)
        {
        }
        if (data != null && data.Length > 0) 
            lblCaption.Text="You are connected.";
        else
            lblCaption.Text="You are not connected.";
    }
}
Don't forget to add System.Net namespace into your page.

The output will be:
Check Internet Connection

Hope now you can check whether user connected to the internet or not.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash