VB.NET Form Blank when Processing

Posted on 2009-12-16
Last Modified: 2012-05-08
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


            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 & "."



        End If

Open in new window

Question by:slightlyoff
    LVL 42

    Assisted Solution

    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.
    LVL 27

    Accepted Solution

    Why don't you checkout the BackGroundWorker?  It's pretty easy to use, and will let your process run on a separate thread:


    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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

    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".
    LVL 1

    Author Comment

    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 :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now