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)
        sqlcon.Open()
        sqlcon.Close()

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

        End If

Recommended Answers

All 4 Replies

im actually interested in this as well..

i normally do

SELECT COUNT(*) FROM TABLE

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

As far as I can see this line is where it hangs:
sda.Fill(dt)
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])
th.SetApartmentState(Threading.ApartmentState.STA)
th.Start()
Member Avatar for Unhnd_Exception

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)

        c.Open()
        da.Fill(DT)
        c.Close()

    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

        DataLoader.EndInvoke(DataLoaderIAsync)

        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})
        Else
            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
                UpdateProgress(counter)
            End If
            counter += 1
        End If

    End Sub

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.