John Smith
John Smith

Reputation: 11

Need make progress bar when data is reading from database

I have a problem with progress bar. I have a button (SELECT * FROM Table).

When I click it, I want to display data in datagridview. It's ok, works fine.

But now I need make a progress bar, while data is reading from DB.

By default progress bar visible should be as false. While reading - progress bar visible is true.

And when data is read from DB, progress bar visible should be false again.

I have a code, but it works incorrect (1. progress bar is buggy and etc.) Maybe you can offer me another variant?

 private void button3_Click(object sender, EventArgs e)
 {
    timer1.Start();         
 }

 private void timer1_Tick(object sender, EventArgs e)
 {
        progressBar1.Visible = true;
        progressBar1.Minimum = 10;
        progressBar1.Maximum = 100;

        progressBar1.Increment(+10);
        if (progressBar1.Value == 90)
        {         
            groupBox3.Enabled = true;
            connect con = new connect();
            DataTable data = connnection.query("SELECT * FROM User WHERE surname LIKE '" + textBox1.Text + "%'");
            dataGridView1.DataSource = tb;

        }
        if(progressBar1.Value == 100)
          progressBar1.Visible = false;
    }

Upvotes: 0

Views: 1897

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

You need to rethink your logic.

When you make that SQL call think of it as code running outside your program, meaning you can only start it up and wait for it to finish, but you won't have access to the internals like how far it is progressing.

What you can do as an alternative is:

  1. Put your SQL call in a BackgroundWorker. Great examples on MSDN and is really pretty easy to use.
  2. When you call the BackgroundWorker, that frees up your UI to run some kind of "Please Wait" control. Since you won't have any idea when the SQL command will end, at least you can put up some kind of control to let the user know that the program is "Processing".
  3. Once the BackgroundWorker's RunWorkerCompleted happens, you can remove your Please Wait and bind up your data.

Upvotes: 1

Related Questions