0

I use the below code to fetch the data from database and display the data on dataGrid now How to Display Custom Progress bar while fetching the data from the database ?

private void ReadRecords(){
        using (SqlConnection c = new SqlConnection(Properties.Settings.Default.Database1ConnectionString)) {
            try{
                c.Open();
                if (c.State == ConnectionState.Open) {
                    using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Items ORDER BY item_id", c)) {
                        try {
                            DataTable t = new DataTable();
                            a.Fill(t);
                            dataGridView1.DataSource = t;
                        }

                        catch(Exception ex){
                            MessageBox.Show("Failed Fill Data :." + ex);
                            return;

                        }
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Failed To Open Connection :." + ex);
                return;
            }
        }

    }
Muhammad Younas
  • 1,543
  • 1
  • 22
  • 32
  • SO won't, or rather shouldn't, just blindly give you code. What I would suggest, since it's a new topic for you, is to take the database out of the equation. Search around on how to get a progress bar on it's own, then work out how to integrate it within your database app. – Arran Dec 20 '14 at 15:40
  • @Arran thanks i try on few codes not working – Muhammad Younas Dec 20 '14 at 15:42
  • You could display a spinner or wait cursor, but something showing percent complete would be difficult. For that you would need to know how long the query will take, and I don't think that information is available. – hatchet - done with SOverflow Dec 20 '14 at 15:43
  • possible duplicate of [Implement progress bar on SQLite database read C#](http://stackoverflow.com/questions/10016090/implement-progress-bar-on-sqlite-database-read-c-sharp) – hatchet - done with SOverflow Dec 20 '14 at 15:47
  • Google for `progress bar backgroundworker C#` – sll Dec 20 '14 at 15:48
  • @Daniel Mann Please open this question so that i ban left from me i really need to ask more question i request you please i now understand how i should ask question that time was my beginner level i shall be really thankful to you after open the question i will delete it Thanks for your considerations – Muhammad Younas Jan 12 '16 at 07:24

2 Answers2

1

The "trick" here is using the UI thread appropriately, if you are loading your data from the DB using the UI thread (typically resulting from a click or load event) then any animation or progress indicator would be frozen because the UI thread is busy with the data operation. What you need to do is thread out or via the TPL "task" out the data loading operation and when that op is complete Invoke a method to turn off the progress indicator.

By threading out the data op to a non-UI thread the UI thread is available to perform your animation. Here is a link that details how to invoke the UI thread:

Execute a delegate in the ui thread (using message pump)

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

The simplest solution is to show loading indicator without any progress values.

ShowProgressIndicator();
ReadRecords()
HideProgressIndicator();

If you want to show real progress, you must determine amount of work. For example let's say total rows count is whole work. You can calculate % of progress by dividing fetched rows / total rows count. You should make several requests to database to implement such progress bar.

First - request total rows count. Then - fetch rows by portions, say 500 rows per request until all rows fetched.

opewix
  • 4,993
  • 1
  • 20
  • 42
  • i want to do like that but how to show ShowProgressIndicator(); and HideProgressIndicator(); is that built in functions or we have to write it ? – Muhammad Younas Dec 20 '14 at 16:27
  • What technology do you use? WPF, Silverlight, WinForms, ConsoleApp or WebApplication (MVC, WebForms)? – opewix Dec 20 '14 at 16:28
  • Windows Forms desktop application with local database..... – Muhammad Younas Dec 20 '14 at 16:50
  • You have to write own methods to animate and stop progress bar. In `ShowProgressIndicator()` method, set progressbar visible and progressbar `Style` property to `Marquee`. – opewix Dec 20 '14 at 16:54
  • @YounasBangash I'm not familiar with WinForms, I've just explained the theory of displaying work progress. Add progress bar control to your form and try to indicate intermediate status setting it's style to `Marquee` – opewix Dec 20 '14 at 17:25