IP and Location getting from table and ping(ms) from code.
Internet Status |
IP | Lacation | Network |
192.168.200.21 |
Cooling Tower |
|
|
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm5.aspx.cs" Inherits="pingtest.WebForm5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
<title></title>
<style type="text/css">
.auto-style2 { text-align: center; font-weight: 700; width: 1000px; }
.auto-style3 { height: 139px; width: 1000px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<table border="1" style="height: 20px; width: 791px">
<tr>
<td class="auto-style2">Internet Status</td>
</tr>
<tr>
<td class="auto-style3">
<asp:GridView runat="server" ID="GridView1" OnRowDataBound="OnRowDataBound" datakey="Id" Height="23px" Width="970px" />
</td>
</tr>
</table>
</form>
</body>
</html>
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.IO;
namespace pingtest
{
public partial class WebForm5 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=10.1.246.4;Initial Catalog=OCMS;User ID=dba_ntpc_meja_intranet;Password=alpha$890;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// string address = "10.1.246.17";
// Label1.Text = ("Pinging" + address);
// long time = PingAddress(address, 1000);
//Label1.Text = PingAddress(address, 1000).ToString();
}
DataTable pingResults = new DataTable();
pingResults.Columns.AddRange(new DataColumn[] {
new DataColumn("IP", typeof(string)),
new DataColumn("Lacation",typeof(string)),
new DataColumn("Network",typeof(string))});
try
{
pingResults.Clear();
List<Employee> employees = GetDetails();
for (int i = 0; i < employees.Count; i++)
{
string address = "IP";
long time = PingAddress(address, 1000);
Ping ping = new Ping();
lock (pingResults.Rows.SyncRoot)
{
pingResults.Rows.Add(employees[i].ipAddress, 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 aaqms", 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;
}
private static long PingAddress(string address, int timeout)
{
try
{
Ping pingClass = new Ping();
PingReply pingReply = pingClass.Send(address, timeout);
return pingReply.RoundtripTime;
}
catch (Exception e)
{
}
return -1;
}
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[3].Text == "On")
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Black;
e.Row.Cells[3].BackColor = System.Drawing.Color.Green;
}
else
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Black;
e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
}
// if (e.Row.Cells[4].Text == "On")
// {
// string date = e.Row.Cells[0].Text;
// string SMSurl = "http://185.255.8.59/sms/1/text/query?username=NTpc&password=NTpc@321&from=MUNPLA&to=919650994376,917763801868,919425823246,919435326427,919415246033,919415501196,917500770555,919415246033,919559695020,919435326427,919667044472.&text=All AAQMS stations are working." + date + "-Meja Urja Nigam PVT. Ltd.&indiaDltContentTemplateId=1207164637771984698&indiaDltPrincipalEntityId=1201159222154902387";
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SMSurl);
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// StreamReader sr = new StreamReader(response.GetResponseStream());
// }
}
}
}
}
}