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