Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using Datagridview with BackgroundWorker in vb.net to simultaneously check the network connectivity status of list of IP addresses but I am getting an error states that Index was outside the bounds of the array after the 1st successful ping.

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.
VB.NET
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
            =

            For Me.i = 0 To IPsConnectionStatusDataGridView.RowCount - 1 'Giving an error --> System.IndexOutOfRangeException was unhandled by user code & HResult = -2146233080 & Message=Index was outside the bounds of the array.



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

                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 21-May-21 5:49am
v3

1 solution

Quote:
VB.NET
Dim i As Integer
Dim reply(i) As PingReply
Here you have defined a field with a default value of 0, and an array of PingReply objects with an upper-bound of 0 (and hence a length of 1).
Quote:
VB.NET
For Me.i = 0 To IPsConnectionStatusDataGridView.RowCount - 1
    reply(i) = ...
In this loop, as soon as i is greater than 0, you are trying to store a value beyond the end of your array. This will throw an IndexOutOfRangeException.

You need to make sure you array is large enough to store all of the results. Once you know the number of addresses you will be pinging, you need to use ReDim[^] to resize the array so that it is large enough.
VB.NET
ReDim reply(IPsConnectionStatusDataGridView.RowCount - 1)
For Me.i = 0 To IPsConnectionStatusDataGridView.RowCount - 1
    reply(i) = ...
Alternatively, you could use the List&(Of T)[^] class, which wouldn't need to know the number of items up-front.
   
Comments
Sherif Adely 2021 22-May-21 8:51am
   
Thanks a lot Richard Deeming

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