• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

VB.NET Form Blank when Processing

I am trying to figure out why when processing a task, my form goes blank.

My program reads the records from database1, then writes a value to database2 and loops until all the records from database1 have been gone through.

I have a basic form set up with a label on it, as well as a button (to execute the code).  My code is attached below.  (The code snipit picks up after i've filled a dataAdapter with all the inventoryItems from QuickBooks (about 3000 skus))

This code works, and successfully updates Database2, but it takes about 4 minutes to complete, and during that time, nothing is displayed on the form.  My buttons, the label for the status - it all clears off the screen.  I should be happy to know that at least the main function of the form is working, but I'd like to see the progress so i know how long it will take to finish as well as if there's a problem.

Any ideas?   Thanks for your help!
If ds.Tables(0).Rows.Count = 0 Then
            'Nothing to Update...             
lblStatus.Text = "QuickBooks is reporting there are no items..."
            Exit Sub
        Else
            Dim y As Integer = ds.Tables(0).Rows.Count - 1
            MsgBox("Number of Records to Update: " & y)
 
            For x = 0 To y
                Sql = "Update products set stock = " & ds.Tables(0).Rows(x).Item("QuantityOnHand") & " where sku = '" & ds.Tables(0).Rows(x).Item("Name") & "'"
                cmd = New SqlCommand(Sql, sqlConn)
                lblStatus.Text = "Now updating record: " & x & " of " & y & "."
                cmd.ExecuteNonQuery()
            Next
        End If

Open in new window

0
slightlyoff
Asked:
slightlyoff
3 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
u want to create a progress dialog for your asynchronous procedure, so the form will be displayed along with a pogressbar dialog which simulate the progress of your task.
0
 
VBRocksCommented:
Why don't you checkout the BackGroundWorker?  It's pretty easy to use, and will let your process run on a separate thread:

    BackGroundWorker:
    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The BackgroundWorker() is definitely the BEST solution.

Your current code ~may~ work if you add DoEvents INSIDE the loop:

             For x = 0 To y
                Sql = "Update products set stock = " & ds.Tables(0).Rows(x).Item("QuantityOnHand") & " where sku = '" & ds.Tables(0).Rows(x).Item("Name") & "'"
                cmd = New SqlCommand(Sql, sqlConn)
                lblStatus.Text = "Now updating record: " & x & " of " & y & "."
                Application.DoEvents() ' <--------------------------  Force it to update screen and process pending events
                cmd.ExecuteNonQuery()
            Next

The main problem is that your database operations are BLOCKING calls so if they are long you may still have an unresponsive and/or "white out" condition on your form.

If the DB operations are relatively short and you just have a ton of them then it may be an acceptable "quick fix".
0
 
slightlyoffAuthor Commented:
Thank you!  It didn't occur to me that i would need to do anything more than what I had written, and i was not familiar with the BackgroundWorker...  that sucker will come in handy!

Thanks :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now