Progressbar with Database select query?

seano

Active member
Joined
Jul 4, 2012
Messages
32
Programming Experience
Beginner
Does any one know how to implement a Progress bar with a select query, Maybe using a Back ground worker? the only thing i dont understand is how to increment the progress bar when running the query(In a loop). any help would be much appreciated!
 
The reason that you don't understand is because it can't be done. You can only report progress that you can measure. If you call Fill on a data adapter then you have no way to measure how far along the task is so you can't report it to the user. Generally, in such situations, you simply set the Style of the ProgresBar to Marquee, to indicate to the user that something is happening but that you don't know how far along it is. You still need to do the data access on a secondary thread though, or the ProgressBar won't animate.

There is one option if you're retrieving data where you can perform the same query twice with the first time getting just the count of the results and then the second time getting the data using a data reader. You then know after reading each record how many have been read and how many there are still to read. The problem with that is that it's more laborious to code but, more importantly, you actually make the whole thing take longer, especially for complex queries.

When you're saving data you also loop through the records and save them one by one, again allowing you to measure you current progress. Again though, you will make the whole process slower by doing so.
 
thanks for the reply mate, how would i get the data access on another thread as i have never worked with threads before? thanks in advanced.
 
Simplest option: use a BackgroundWorker. Perform your data access in the DoWork event handler and call RunWorkerAsync to set it off. If you need to prepare the UI then do so before calling RunWorkerAsync. If you need to update the UI along the way then call ReportProgress and handle the ProgressChanged event. If you need to update the UI when you're done then handle the RunWorkerCompleted event. Here's one I made earlier:

Using the BackgroundWorker Component
 
So the data Retrieval code goes in the for loop?

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _

ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)



For i As Integer = 1 To 100


'Raise the ProgressChanged event in the UI thread.


worker.ReportProgress(i, i & " iterations complete")



Dim DSObject As New DataSet
Dim DCON As OleDbConnection
'create and open the data connection
DCON = New OleDbConnection(connectionstring)
Try
DCON.Open()
If rbStart.Checked Then
'Run select query with start of feild wildcards
' Data Adaptor with connection to show all from table ^
Dim DAPT As New OleDbDataAdapter("SELECT * FROM CatalistT WHERE [" & Feild & "] LIKE '" & txtsearch.Text & "%'", DCON)
'Put everything from Data Adapter into DATA SET
DAPT.Fill(DSObject, "CatalistT")


Threading.Thread.Sleep(250)

Next i


End Sub

But wouldnt that run the Data Retrieval code 100 times?

 
Back
Top