Hi,
I use backgroundWorker in order to execute stored procedures on SQL Server.
It works OK, but since all stored procedures in GUI are shown in treeView as root nodes I would like to add child node to each executed sp after the execution of the same is completed (for eg. I would like to add node named "OK - done").
Here is my vb code I use to run procedures but try to add nodes with no success. I marked position where I would like to do this marked as ' HERE I WOULD LIKE TO ADD NODE TO ITEM (WHICH REPRESENTS SP)
Thanks a lot for help
A
Private Sub ToolStripButton11_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton11.Click
Dim clbItems As New List(Of String)
For Each item As TreeNode In TreeView4.Nodes
clbItems.Add(item.Text)
Next
If BackgroundWorker1.IsBusy Then
ToolStripButton11.Enabled = False
ToolStripStatusLabel1.Text = "Canceling..."
BackgroundWorker1.CancelAsync()
Else
ToolStripButton11.Text = "Cancel"
ToolStripStatusLabel1.Text = "Running..."
BackgroundWorker1.RunWorkerAsync(clbItems)
End If
End Sub
Private Sub BackgroundWorker1_DoWork_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim bwAsync As BackgroundWorker = TryCast(sender, BackgroundWorker)
Dim clbItems As List(Of String) = CType(e.Argument, List(Of String))
Dim item As String
Dim con As New SqlConnection(My.Settings.SQLServerConn)
con.Open()
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandTimeout = 0
cmd.CommandType = CommandType.StoredProcedure
For Each item In clbItems
bwAsync.ReportProgress(0, item)
If bwAsync.CancellationPending Then
Thread.Sleep(1200)
e.Cancel = True
Return
End If
cmd.CommandText = item
cmd.ExecuteNonQuery()
Next
con.Close()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ToolStripButton11.Text = "Run Stored procedures"
ToolStripButton11.Enabled = True
If e.[Error] IsNot Nothing Then
MessageBox.Show(e.[Error].Message)
Return
End If
If e.Cancelled Then
ToolStripStatusLabel1.Text = "Cancelled..."
Else
ToolStripStatusLabel1.Text = "Completed..."
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ToolStripStatusLabel1.Text = e.UserState.ToString
End Sub