I want to use a progressbar to show that I'm retrieving the records from the database so that the user wont be confuse if the program is still running or not. Here's my code. Please help me..

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cs = "Initial Catalog = KRADB; Data Source = .; User id = lab; Password = 'clrscr2010'"
        sqlcon = New SqlConnection(cs)

        dt = New DataTable
        sda = New SqlDataAdapter("SELECT * FROM KRAWEEK WHERE STATUS='INITIAL'", sqlcon)
        If dt.Rows.Count <> 0 Then
            dg.DataSource = dt

        End If
6 Years
Discussion Span
Last Post by Lee21

im actually interested in this as well..

i normally do


then i set the progress bars max value to the count(*)

and whenever i begin my loading process, each row that i return i increase the value + 1

im sure there is a more efficient way though, just dont know of it

Edited by jlego: n/a


As far as I can see this line is where it hangs:
So the only way you can also run a progress bar is to have either this or the progress bar running on another thread.
And that's fun.

Dim th As New System.Threading.Thread(AddressOf [B]YourProgressBarRoutine[/B])

Heres 1 of many ways to accomplish that.

Private WithEvents DT As New DataTable
    Private Delegate Sub AsyncDelegate(ByVal value As Integer)
    Private ProgressUpdater As New AsyncDelegate(AddressOf UpdateProgress)
    Private DataLoader As AsyncDelegate
    Private DataLoaderIAsync As IAsyncResult
    Private UpdateIncrement As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Initialize the progress bar.

        'Run a scalar command to select the row count and set the progress maximum

        'Calculate a percentage for the Row Changed event.
        '  ie if total recs is 1,000,000 set the progress amount to 10% of that.
        '  *See DT_RowChanged

        UpdateIncrement = 100000

        'DeEnable any controls you may not want the user to touch until the 
        'load is complete.

        'Load the data Ascync
        DataLoader = New AsyncDelegate(AddressOf LoadData)
        DataLoaderIAsync = DataLoader.BeginInvoke(0, New AsyncCallback(AddressOf LoadData_Completed), Nothing)

    End Sub

    Private Sub LoadData()

        'Your Load data code.
        'This sample code loads 1,000,000 recs

        'Do not access any controls in the procedure
        Dim c As New SqlCeConnection("Data Source=C:\Users\Administrator\Documents\Visual Studio 2008\Projects\Combobox\Combobox\Database1.sdf")
        Dim cm As New SqlCeCommand("Select * From Test", c)
        Dim da As New SqlCeDataAdapter(cm)


    End Sub

    Private Sub LoadData_Completed()

        'Async op has completed.
        'Call the endinvoke to the delegate and unlock the UI

        'Do not access any controls in this procedure


        Me.Invoke(New AsyncDelegate(AddressOf UnlockUI), New Object() {Nothing})

    End Sub

    Private Sub UnlockUI()
        'Do what you want to the UI Here.
        ProgressBar1.Visible = False

    End Sub

    Private Sub UpdateProgress(ByVal value As Integer)
        'Updates the progress bar from the thread it was
        'created on.
        If ProgressBar1.InvokeRequired Then
            ProgressBar1.Invoke(ProgressUpdater, New Object() {value})
            If value > ProgressBar1.Maximum Then value = ProgressBar1.Maximum
            ProgressBar1.Value = value
        End If

    End Sub

    Private Sub DT_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles DT.RowChanged
        'Handle a row change
        'Do not access any controls from this procedure

        Static counter As Integer = 1

        'first make sure the row is being added
        If e.Action = DataRowAction.Add Then
            'next check for an increment.  In this case I have a hard coded
            '100,000 Which is 10% of the million records that I know I have.

            'Do not update the progress bar on every record

            If counter Mod UpdateIncrement = 0 Then
            End If
            counter += 1
        End If

    End Sub

Edited by Unhnd_Exception: n/a


thanks for your replies, I'll try to work on it..

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.