VB App Listview Issue

  • Thread starter Thread starter ckeenum23
  • Start date Start date
C

ckeenum23

Guest
First off, I just want to say thank you to the VB community. I really want to get into coding as my main stay. The fact that you guys are here to help willingly says alot about how great the community really is. Much appreciated. This is my first post, with many more to come. Over time, I hope to be one of the guys replying to the new kid on the block with the questions. "If you have done well in whatever business you are in, it is your duty to send the elevator back down and try to help bring up the next generation of undiscovered talent." -Kevin Spacey

With that being said, this is what I am trying to accomplish. I have a handful of servers that like to drop off of our DNS server on a daily basis. What I am trying to do is make an application that will read in our server names and their IPs from a text file named servers.txt. So, two columns in my ListView1. I have that part working perfectly with a delimiter to separate each column.

servers.txt contents:

SERVERNAME1=192.168.1.1
SERVERNAME2=192.168.1.2
SERVERNAME3=192.168.1.3
etc...



What Ive done for now to visualize things is Ive created 3 ListViews: Listview1 reads in the servers.txt file, Listview 2 displays successes, and Listview 3 displays failures. After that completes, I have a button to export the contents of Listview3 to a CSV file where I will run a PowerShell command against it to add the servers back to DNS.

Borrowing the code from: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c4a4ce86-3645-485f-b537-2eb411c77978/ping-listview-ips-vbnet?forum=vbgeneral works, but when I try to add my servers from ListView1 I get an error of "System.ArgumentNullException: Value cannot be null.". The strange thing is that it splits the servers between Listview2 and Listview3. Showing me what I need to know. I just want to get the error corrected if possible. Also, the app is only adding my server name to ListView 2 and 3. I really need both Server Name and IP to copy over. I havent figured that part out yet.

Any help would be much appreciated. Thanks!

Heres the complete code:

Imports System.Net.NetworkInformation
Imports System.IO
Public Class Form1
Public mPingAddresses

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

ListView1.View = View.Details
ListView1.Columns.Add("Server", 150)
ListView1.Columns.Add("IP", 130)

ListView2.View = View.Details
ListView2.Columns.Add("Server", 150)
ListView2.Columns.Add("IP", 130)

ListView3.View = View.Details
ListView3.Columns.Add("Server", 150)
ListView3.Columns.Add("IP", 130)

Try
mPingAddresses = New List(Of String)
Initialise the list of addresses...
mPingAddresses.Add("192.168.1.1")
mPingAddresses.Add("192.168.1.2")
mPingAddresses.Add("192.168.1.3")
mPingAddresses.Add("192.168.1.4")
mPingAddresses.Add("192.168.1.5")
Catch ex As Exception
MsgBox("Error in loading the servers into the List view. Details: " & ex.ToString)
End Try

Try
Declare StreamReader
Dim MyStream As New StreamReader("C:\test\servers.txt")
A string array to hold each line as it is read
Dim strTemp() As String
Code that reads the file line by line
Remove outside array error
Do While MyStream.Peek >= 0 Use Peek to read the file until there are no more lines
Dim LVItem As New ListViewItem
Split the line using the - delimiter
strTemp = MyStream.ReadLine.Split("="c)
Assign the content of the first element of the array to the first column in the LV
LVItem.Text = strTemp(0).ToString
Then add the item to the LV
ListView1.Items.Add(LVItem)
mPingAddresses.Add(LVItem.Text)
Assign the content of the 2nd element to the next column in the LV
LVItem.SubItems.Add(strTemp(1).ToString)
Check the length of columns
If strTemp.Length > 2 Then LVItem.SubItems.Add(strTemp(2).ToString)
Loop

Close the StreamReader
MyStream.Close()
Catch ex As Exception
MessageBox.Show("Error reading file." & ex.Message)
End Try

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Clear any existing items
Me.ListBox1.Items.Clear()
Loop through the addresses generating an aysnc ping...
For Each ipAddy As String In mPingAddresses
Generate the request
Dim myPing As New Net.NetworkInformation.Ping()
Add the handler for this request...
AddHandler myPing.PingCompleted, AddressOf PingRequestCompleted
myPing.SendAsync(ipAddy, ipAddy)
Next
Catch ex As Exception
MsgBox("Error in the initial NetworkInformation.Ping section. Details: " & ex.ToString)
End Try

End Sub

Private Sub btnCSV_Click(sender As Object, e As EventArgs) Handles btnCSV.Click
Exports VMs not in DNS to CSV file
Try
Dim headers = (From ch In ListView3.Columns
Let header = DirectCast(ch, ColumnHeader)
Select header.Text).ToArray()

Dim items() = (From item In ListView3.Items
Let lvi = DirectCast(item, ListViewItem)
Select (From subitem In lvi.SubItems
Let si = DirectCast(subitem, ListViewItem.ListViewSubItem)
Select si.Text).ToArray()).ToArray()

Dim table As String = String.Join(",", headers) & Environment.NewLine
For Each a In items
table &= String.Join(",", a) & Environment.NewLine
Next
table = table.TrimEnd(CChar(vbCr), CChar(vbLf))
IO.File.WriteAllText("C:\test\servers.csv", table)
Catch ex As Exception
MsgBox("Error in the Export to CSV section: " & ex.ToString)
End Try

End Sub

Public Sub PingRequestCompleted(ByVal sender As Object, ByVal e As Net.NetworkInformation.PingCompletedEventArgs)

Try
When received, add the approrpiate entry into the listbox
Me.ListView1.Items.Add(e.UserState.ToString & " " & e.Reply.Status)
If e.Reply.Status = IPStatus.Success Then
ListView2.Items.Add(e.UserState.ToString)
ListView2.ForeColor = Color.Green
Else
ListView3.Items.Add(e.UserState.ToString)
ListView3.ForeColor = Color.Red
End If
Catch ex As Exception
MsgBox("Error in the Ping Request status section. Details: " & ex.ToString)
End Try

End Sub

End Class


Feel free to use any of this code for your personal projects.

Continue reading...
 
Back
Top