Progress reporting via background Worker while reading XML File

  • Thread starter Thread starter Coderv9
  • Start date Start date
C

Coderv9

Guest
Hi am trying to read a XML file and load it into a datagrid view. Since the XML contains thousands of entries i want users to show the progress on a label.

But it gives the strange error as below.

1421314.png


Below is my Code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
'' call this method to start your asynchronous Task.
BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim sccm_srv_link As String
Try
sccm_srv_link = "Http://example.com/XML_URL"
dg_machine.ColumnCount = 6
dg_machine.Columns(0).Name = "Host Name"
dg_machine.Columns(1).Name = "Service Tag"
dg_machine.Columns(2).Name = "Model"
dg_machine.Columns(3).Name = "User Name"
dg_machine.Columns(4).Name = "User ID"
dg_machine.Columns(5).Name = "Location"
Dim dg_row As Integer = 0
Dim first_row As Boolean = True
Dim xmlfile As XmlReader
Dim resolver As New XmlUrlResolver
Dim mycred As System.Net.NetworkCredential
mycred = New System.Net.NetworkCredential("UserNAme", "Password", "DOMAIN")
resolver.Credentials = mycred
Dim xmsettings As New XmlReaderSettings
xmsettings.XmlResolver = resolver
xmlfile = XmlReader.Create(sccm_srv_link, xmsettings)
While xmlfile.Read()
Select Case xmlfile.NodeType
Case XmlNodeType.Element 'Display beginning of element.
If xmlfile.Name = "Details" Then
If xmlfile.HasAttributes Then 'If attributes exist
While xmlfile.MoveToNextAttribute()
If xmlfile.Name = "Device_Name" Then
If first_row = True Then
dg_row = 0
first_row = False
Else
dg_row = dg_row + 1
End If
dg_machine.Rows.Add()
dg_machine.Rows(dg_row).Cells(0).Value = xmlfile.Value
End If
If xmlfile.Name = "Serial_Number" Then
dg_machine.Rows(dg_row).Cells(1).Value = xmlfile.Value
End If
If xmlfile.Name = "Model" Then
dg_machine.Rows(dg_row).Cells(2).Value = xmlfile.Value
End If
If xmlfile.Name = "Full_Name" Then
dg_machine.Rows(dg_row).Cells(3).Value = xmlfile.Value
End If
If xmlfile.Name = "User_Name" Then
dg_machine.Rows(dg_row).Cells(4).Value = xmlfile.Value
End If
If xmlfile.Name = "Location" Then
dg_machine.Rows(dg_row).Cells(5).Value = xmlfile.Value
End If
End While
End If
End If
End Select
End While
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Label1.Text = CType(e.UserState, String)
Label2.Text = e.ProgressPercentage.ToString & "% complete."
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show(e.Error.Message)
Label1.Text = "Error occurred!"

ElseIf e.Cancelled Then
MessageBox.Show("Task cancelled!")
Label1.Text = "Task Cancelled!"

Else
MessageBox.Show("Task completed!")
Label1.Text = "Error completed!"
End If
Button1.Enabled = True
Button2.Enabled = False
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Button2.Enabled = False
BackgroundWorker1.CancelAsync()
End Sub





Coderv9

Continue reading...
 

Similar threads

V
Replies
0
Views
259
Vergassivellaunus
V
V
Replies
0
Views
285
Vergassivellaunus
V
Back
Top