Click here to Skip to main content
15,895,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am using Datagridview with BackgroundWorker in vb.net to simultaneously check the network connectivity status of list of IP addresses and it’s working properly if the Ping status of all IPs is Success but I am getting an error states Object reference not set to an instance of an object at the 1st IP with Ping status is Timed Out.

Any idea what could be causing it and how I can overcome it?

Your kind support is highly appreciated.

What I have tried:

Below is my full code.

Imports System.ComponentModel
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Threading

Public Class Form17

Dim Ping As New Ping
Dim i As Integer
Dim reply(i) As PingReply

Private Sub Form17_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'IPSDataDataSet.IPsConnectionStatus' table. You can move, or remove it, as needed.
Me.IPsConnectionStatusTableAdapter.Fill(Me.IPSDataDataSet.IPsConnectionStatus)

BackgroundWorker1.RunWorkerAsync()


End Sub

Private Sub IPsConnectionStatusBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles IPsConnectionStatusBindingNavigatorSaveItem.Click
Me.Validate()
Me.IPsConnectionStatusBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.IPSDataDataSet)

End Sub


Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork


Thread.Sleep(1000)

Me.IPsConnectionStatusDataGridView.DataSource = Me.IPsConnectionStatusBindingSource

Try

ReDim reply(IPsConnectionStatusDataGridView.RowCount - 1)
For Me.i = 0 To IPsConnectionStatusDataGridView.RowCount - 1


'==================================

reply(i) = Ping.Send(IPsConnectionStatusDataGridView(1, i).Value, 1000)
IPsConnectionStatusDataGridView(2, i).Value = reply(i).Status.ToString
IPsConnectionStatusDataGridView(3, i).Value = reply(i).RoundtripTime
IPsConnectionStatusDataGridView(6, i).Value = reply(i).Address.ToString() 'Address


Next



Catch ex As Exception

MsgBox(ex.Message)
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End If

End Try
End Sub


Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

Try


BackgroundWorker1.RunWorkerAsync()

'========================================================
If IPsConnectionStatusDataGridView(2, i).Value = "Success" Then
IPsConnectionStatusDataGridView(4, i).Value = Now ' 'Last Succeed On

End If

'================================================================
If IPsConnectionStatusDataGridView(2, i).Value = "TimedOut" Then
IPsConnectionStatusDataGridView(5, i).Value = Now ' 'Last Failed On

End If

Catch ex As Exception

MsgBox(ex.Message)
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End If


End Try
End Sub



End Class
Posted
Updated 22-May-21 6:06am

1 solution

VB
reply(i) = Ping.Send(IPsConnectionStatusDataGridView(1, i).Value, 1000)
IPsConnectionStatusDataGridView(2, i).Value = reply(i).Status.ToString
IPsConnectionStatusDataGridView(3, i).Value = reply(i).RoundtripTime
IPsConnectionStatusDataGridView(6, i).Value = reply(i).Address.ToString() 'Address

You are assuming that each element of reply contains a valid reference. But it may not, so check first before you use it as a reference.

See IPStatus Enum (System.Net.NetworkInformation) | Microsoft Docs[^].
 
Share this answer
 
v2
Comments
Sherif Adely 2021 22-May-21 12:47pm    
Thanks Richard but my code is working properly if the Ping status of all IPs is Success but I am getting this error only at the 1st IP with Ping status is Timed Out.So how I can overcome that.
Richard MacCutchan 22-May-21 16:14pm    
Do what I already suggested, and check the status of the reply. Don't assume that every field will contain the information you think.

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