Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone please help me with C# Code to ping a list of IP's stored in SQL Database Table and store the result in the same database table and display it using GridView . Also the status of the systems should be checked after certain interval of time.
Posted
Comments
Prakruti_s06 15-Mar-15 5:05am    
This code works for a single IP.

Ping ping = new Ping();
PingReply pingresult = ping.Send("10.10.160.5");
if (pingresult.Status.ToString() == "Success")
{
Response.Write("Success");
}

I want to ping a list of different IP's stored in the database table and store the result in the same database table, then display the same in a Grid View

First of all, as you will probably have more than just a few addresses to ping, checkig them one after the other would consume much time. Paralleism is just for you. See this article: http://www.aspsnippets.com/Articles/Parallelism---Parallel-Task-Execution-in-.Net-4.0.aspx[^]

On the other hand, asp.net and the web in general is stateless. Yor web applicatin will be asleep as long as nobody is requesting anything from it. Thus doing scheduled tasks in a web application is not really a good a good approach - if hosted in a regular web server, like IIS. But you have also WebAPI at hands and you can host your web service inside your own windows service for example (see: ASP.NET WebAPI Hosting Techniques[^]).

So you either
A) make a simple windows service to ping thos host at specified intervals, ans use IIS as web server
B) use windows task scheduler to call your console application which does the same, and use IIS as web server
C) make a windows service that does the ping, and in the same time it provides the web service.

This latest one is the most complicated to make, but it is the easiest to manage as a product.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Mar-15 9:14am    
5ed.
—SA
Prakruti_s06 15-Mar-15 11:46am    
Thanks for the suggestion, I'm using IIS Server here. I would be hosting this site on remote server using IIS, hence wish to know the code for pinging multiple IP address and displaying their status in a Grid View.
Zoltán Zörgő 15-Mar-15 14:46pm    
The code you can use to ping them in parallel is on the link I have posted, but you will need to adapt it to get the addresses from a table and to store the result in it.

And no, I won't give you that code. Because this is not how it works here. Try to solve it on your own. And if you get struck, come back and ask for help in that concrete situation - starting a new topic.
Prakruti_s06 15-Mar-15 14:53pm    
No Issues, thanks for your valuable time and help.
As you already have the code for a single IP, you just need to run a loop with all the IPs you get from the database.

So, get the IPs from the database table and run a loop. Inside the loop do as you are doing with the IP. Also, just after you get the result, connect with the database and store the result for that particular IP according to your logic.
 
Share this answer
 
Comments
Prakruti_s06 15-Mar-15 11:44am    
Thanks for the suggestion, I had the same thing in mind and I tried but that doesn't work really. I want to know how exactly this would work
What have you tried and where exactly is the issue?
Prakruti_s06 15-Mar-15 14:57pm    
I tried looping this code


Ping ping = new Ping();
PingReply pingresult = ping.Send("10.10.160.5");
if (pingresult.Status.ToString() == "Success")
{
Response.Write("Success");
}

But, I do not exactly know, how do I pass multiple ip addresses from the database in place of this statement
PingReply pingresult = ping.Send("10.10.160.5");
Actually you have to run a query to get all the IPs from the Database Table. You can use SQLDataAdapter and get the records and bind it to a DataTable or DataSet. Then loop through the rows of that DataTable/DataSet and get the IP from the row inside the Loop.

Refer - SqlDataAdapter Class. See the code snippet given to connect and fill the results to a DataSet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900