Retrieve (Get) IP Address from database and check status is online or offline using C# and VB.Net in

Last Reply 3 days ago By dharmendr

Posted 3 days ago

Retrieve (Get) More then 200 IP Address from database and check status is online or offline using C# and VB.Net in ASP.Net

I have more then 200 employeed details. while i have insert all data in table then details not showing ..

.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration;

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

            DataTable pingResults = new DataTable();
            pingResults.Columns.AddRange(new DataColumn[] {
                new DataColumn("Date", typeof(DateTime)),
                new DataColumn("IP", typeof(string)),
                new DataColumn("MacAddress",typeof(string)),
                new DataColumn("MachineName",typeof(string)),
                new DataColumn("Message",typeof(string)),
                new DataColumn("Emp_Name",typeof(string))});
            try
            {
                pingResults.Clear();

                List<Employee> employees = GetDetails();

                for (int i = 0; i < employees.Count; i++)
                {
                    Ping ping = new Ping();
                    PingReply pingReply = ping.Send(employees[i].ipAddress);
                    string message = (pingReply.Status == IPStatus.Success) ? "On" : "Off";
                    lock (pingResults.Rows.SyncRoot)
                    {
                        pingResults.Rows.Add(DateTime.Now, employees[i].ipAddress, GetMacAddress(employees[i].ipAddress), GetMachineName(employees[i].ipAddress), message, employees[i].Emp_Name);
                    }
                    Thread.Sleep(100);
                }
                GridView1.DataSource = pingResults;
                GridView1.DataBind();

            }
            catch (Exception ex)
            {

            }
        }

        public class Employee
        {
            public string ipAddress { get; set; }

            public string Emp_Name { get; set; }
        }


        private List<Employee> GetDetails()
        {
            List<Employee> employees = new List<Employee>();
            string cs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT emp_nm,ip_add FROM emp_ip", con))
                {
                    con.Open();
                    SqlDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                    {
                        employees.Add(new Employee
                        {
                            Emp_Name = sdr["emp_nm"].ToString(),
                            ipAddress = sdr["ip_add"].ToString()
                        });
                    }
                    con.Close();
                }
            }

            return employees;
        }

        public string GetMacAddress(string ipAddress)
        {
            string macAddress = string.Empty;
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "arp";
            pProcess.StartInfo.Arguments = "-a " + ipAddress;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow = true;
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();
            string[] substrings = strOutput.Split('-');
            if (substrings.Length >= 8)
            {
                macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                            + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                            + "-" + substrings[7] + "-"
                            + substrings[8].Substring(0, 2);
                return macAddress;
            }
            else
            {
                return "not found";
            }
        }

        private string GetMachineName(string ipAdress)
        {
            string machineName = string.Empty;
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
                machineName = hostEntry.HostName;
            }
            catch (Exception ex)
            {

            }
            return machineName;
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (!string.IsNullOrEmpty(e.Row.Cells[4].Text))
                {
                    if (e.Row.Cells[4].Text == "On")
                    {
                        e.Row.Cells[4].ForeColor = System.Drawing.Color.Black;
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        e.Row.Cells[4].ForeColor = System.Drawing.Color.Black;
                        e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
    }
}
    
.aspx
<%@ Page Language="C#" AutoEventWireup="true" Codefile="employee.aspx.cs" Inherits="intnt_mntrng_sys.employee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
  <asp:GridView runat="server" ID="GridView1" OnRowDataBound="OnRowDataBound" />
    </form>
</body>
</html>

 

CREATE TABLE [dbo].[emp_ip] (
    [Id]     INT            IDENTITY (1, 1) NOT NULL,
    [emp_id] NCHAR (10)     NULL,
    [emp_nm] NCHAR (50)     NULL,
    [ip_add] NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_emp_ip] PRIMARY KEY CLUSTERED ([Id] ASC)
);

 

You are viewing reply posted by: dharmendr 3 days ago.
Posted 3 days ago

Please use try catch and check the error if any in the catch block.