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;
            }
        }

    }
6
  • 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. Commented Dec 20, 2014 at 15:40
  • @Arran thanks i try on few codes not working Commented Dec 20, 2014 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. Commented Dec 20, 2014 at 15:43
  • possible duplicate of Implement progress bar on SQLite database read C# Commented Dec 20, 2014 at 15:47
  • Google for progress bar backgroundworker C# Commented Dec 20, 2014 at 15:48

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

i want to do like that but how to show ShowProgressIndicator(); and HideProgressIndicator(); is that built in functions or we have to write it ?
What technology do you use? WPF, Silverlight, WinForms, ConsoleApp or WebApplication (MVC, WebForms)?
Windows Forms desktop application with local database.....
You have to write own methods to animate and stop progress bar. In ShowProgressIndicator() method, set progressbar visible and progressbar Style property to Marquee.
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.