Thursday, 29 August 2013

Check Internet Connection availability in ASP.Net, Check Internet Connection in Asp.net using VB.NET /C#, how to check the internet connectivity using ASP.NET, how to Test Internet Connectivity

Hi friends, in this article I will explain about Check Internet Connection availability in ASP. Net,  Check Internet Connection in Asp.net using VB.NET /C#, how to check the internet connectivity using ASP.NET or how to Test Internet Connectivity.
I will explain about Check Internet Connection availability in ASP.NET by downloading my website from the server. Below is the code to Check Internet Connection availability in ASP.NET using VB.NET or C#.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Aspdotnet-Roja:Check Internet Connection Availability using ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Check Internet Connectivity" Height="32px" Width="264px" />
        <asp:Label ID="netconlbl" runat="server" Text="Label" ></asp:Label>
    </div>
    </form>
</body>
</html>

Then Import Below namespaces.
Imports System
Imports System.Net

In VB.NET:
Imports System
Imports System.Net
Partial Class netcon
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        netconlbl.Visible = False
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim client As New WebClient()
        Dim datasize As Byte() = Nothing
        Try
            datasize = client.DownloadData("http://aspdotnet-roja.blogspot.com")
        Catch ex As Exception
        End Try
        netconlbl.Visible = True
        If datasize IsNot Nothing AndAlso datasize.Length > 0 Then
            netconlbl.Text = "Internet Connection Available."
        Else
            netconlbl.Text = "Internet Connection Not Available."
        End If
    End Sub
End Class

In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net;
partial class netcon : System.Web.UI.Page
{
                protected void Page_Load(object sender, System.EventArgs e)
                {
                                netconlbl.Visible = false;
                }
                protected void Button1_Click(object sender, System.EventArgs e)
                {
                                WebClient client = new WebClient();
                                byte[] datasize = null;
                                try {
                               datasize = client.DownloadData("http://aspdotnet-roja.blogspot.com");
                                } catch (Exception ex) {
                                }
                                netconlbl.Visible = true;
                                if (datasize != null && datasize.Length > 0) {
                                                netconlbl.Text = "Internet Connection Available.";
                                } else {
                                                netconlbl.Text = "Internet Connection Not Available.";
                                }
                }
    Public netcon()
                {
                                Load += Page_Load;
                }
}


Run the above page. The output of above page is as shown in the below figure.


Thanks for reading my Aspdotnet-Roja blog, please give me your comment or feedback. Keep reading and sharing your knowledge!

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.