locked
Display Progressbar while dataset loads RRS feed

  • Question

  •  

    Hi:

    I'm new to Visual Basic, new to writing code, actually. I'm using Visual Basic 2005 and SQL Server 2005. I'm filling a dataset named dstblnewresults that typically loads approximately 800,000 rows.  The data is then displayed in a datagrid view named dGvNewResults. I'd like to display a progressbar while the rows ares being added. I've searched the Web extensively and have not been able to find a clear, concise example. Can anyone provide me with a detailed explanation of how to accomplish this? Thank you so very much in advance!

    Thursday, August 7, 2008 4:06 PM

Answers

  • Hi CarmineD,

     

    As far as I can understand, the problem you are facing is “How to use ProgressBar to trace the process of filling a dataset”.

     

    As JohnGrove has provided a way to show the ProgressBar in Marquee style, you will see no effect when you put “me.progressbar1.visible=true…” in constructor or Load event handler. Because all the code in the load event handler will be executed before the shown event is fired.  So when the form is shown, the value of progressbar.visible is false.  You can put the code in a button click event handler.

     

    As far as I know, if we use DataAdapter.fill(dataset) to fill the dataset, it will be difficult for us to trace of exact process of the filling. However, we can use Command.ExcuteXXX() to execute query and add the record to the Dataset. Indeed we would write more codes, but the process can be easily traced. In my test project, I use the following steps and it does the job well.

    1.       Get the total row number of the DataTalbe.(how to get? Refer to code1)

    2.       Set the minimum and maximum property of ProgressBar(how to set? Refer to code2)

    3.       Execute the query using Command.ExecuteReader(), then add the records to the DataTable.(how to? Refer to code3)

    Code1:

    Code Snippet

    static SqlConnection conn = new SqlConnection(@"Data Source=MS-31AC161FD490\ENTERPRISE;Initial Catalog=MyDB;Integrated Security=True");

    static SqlCommand cmd1 = new SqlCommand("select count(*) as RowNumber from Table1", conn);

                    SqlDataReader da1 = cmd1.ExecuteReader();

                    da1.Read();

                    int RowNumber = (int)da1[0];

                    da1.Close();

     

     

    Code 2:

    Code Snippet

                    this.progressBar1.Minimum = 0;

                    this.progressBar1.Maximum = RowNumber;

     

     

    Code3:

    Code Snippet

    private SqlCommand cmd = new SqlCommand("select * from Table1", conn);

                    SqlDataReader da = cmd.ExecuteReader();

                    DataTable dt = new DataTable();

                    

                    dt.Columns.Add("column1", typeof(string));

                    dt.Columns.Add("column2", typeof(string));

                    dt.Columns.Add("column3", typeof(string));

                    dt.Columns.Add("column4", typeof(int));

    this.progressBar1.Value = 0;

                    while (da.Read())

                    {

                        DataRow tempRow = dt.NewRow();

                        tempRow[0] = da[0];

                        tempRow[1] = da[1];

                        tempRow[2] = da[2];

                        tempRow[3] = da[3];

                        dt.Rows.Add(tempRow);

                        this.progressBar1.Value++;

                    }

                    ds.Tables.Add(dt);

                    // sda.Fill(ds);

                    this.dataGridView1.DataSource = ds.Tables[0];

     

     

    Note: If these codes were put in constructor or load event handler, the effect will not be seen.

     

    If you have difficulty in understanding c# language, please use the convert tool to have a translation: http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx.

     

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Tuesday, August 12, 2008 5:10 AM
  • Thank you so very much, Bruce. I will attempt to use that code. It looks straightforward and as if it should work. In the meantime I've temporarily solved my problem with a very simple solution.

    I created a second form for my project and placed a progressbar on the form. I then selected the "properties" for the project, set my primary form as the startup form, and designated the second form as the "Spalsh screen."

    It works like a charm. The second form with the progressbar stays visible until the dataset is filled, the dataset bound to the datagridview, then the splash screen closes.

    Thank you all for your advice!

     

    Tuesday, August 12, 2008 10:53 AM

All replies

  • I'm not sure how you would set the Min/Max values on filling a DataSet, but you could set the progress bar style to Marquee and not worry about that. That gives the user some indication something is happening and to stand by.

    Thursday, August 7, 2008 5:17 PM
  • Thank you for the response, John. Unfortunately, I believe I'm too new to this to make any use of your suggestion. I placed a progressbar named progressbar1 on my form. The dataset to which I referred in my initial post is filled during the form load event. So I configured progressbar style to marquee and entered a line directly above the dataset fill reading: me.progressbar1.visible = True. After the dataset fill line I entered me.progressbar1.visible = False. However, when I ran the application (debug mode) the form does not appear, nothing happens, in fact, until the dataset is loaded and bound to my datagridview. This is the same result I get without the progressbar, which is why I wanted a progressbar; so the user would know something is happening, that the application wasn't locked up. Can you please elaborate on your suggestion? Thank you again.

    Thursday, August 7, 2008 9:20 PM
  • What you should do is:

     

    Me.ProgressBar1.Show()

    Me.ProgressBar1.Hide()

     

    If this doesn't work, you will need to use the backGroundWorker control

    Thursday, August 7, 2008 9:26 PM
  • Hi CarmineD,

     

    As far as I can understand, the problem you are facing is “How to use ProgressBar to trace the process of filling a dataset”.

     

    As JohnGrove has provided a way to show the ProgressBar in Marquee style, you will see no effect when you put “me.progressbar1.visible=true…” in constructor or Load event handler. Because all the code in the load event handler will be executed before the shown event is fired.  So when the form is shown, the value of progressbar.visible is false.  You can put the code in a button click event handler.

     

    As far as I know, if we use DataAdapter.fill(dataset) to fill the dataset, it will be difficult for us to trace of exact process of the filling. However, we can use Command.ExcuteXXX() to execute query and add the record to the Dataset. Indeed we would write more codes, but the process can be easily traced. In my test project, I use the following steps and it does the job well.

    1.       Get the total row number of the DataTalbe.(how to get? Refer to code1)

    2.       Set the minimum and maximum property of ProgressBar(how to set? Refer to code2)

    3.       Execute the query using Command.ExecuteReader(), then add the records to the DataTable.(how to? Refer to code3)

    Code1:

    Code Snippet

    static SqlConnection conn = new SqlConnection(@"Data Source=MS-31AC161FD490\ENTERPRISE;Initial Catalog=MyDB;Integrated Security=True");

    static SqlCommand cmd1 = new SqlCommand("select count(*) as RowNumber from Table1", conn);

                    SqlDataReader da1 = cmd1.ExecuteReader();

                    da1.Read();

                    int RowNumber = (int)da1[0];

                    da1.Close();

     

     

    Code 2:

    Code Snippet

                    this.progressBar1.Minimum = 0;

                    this.progressBar1.Maximum = RowNumber;

     

     

    Code3:

    Code Snippet

    private SqlCommand cmd = new SqlCommand("select * from Table1", conn);

                    SqlDataReader da = cmd.ExecuteReader();

                    DataTable dt = new DataTable();

                    

                    dt.Columns.Add("column1", typeof(string));

                    dt.Columns.Add("column2", typeof(string));

                    dt.Columns.Add("column3", typeof(string));

                    dt.Columns.Add("column4", typeof(int));

    this.progressBar1.Value = 0;

                    while (da.Read())

                    {

                        DataRow tempRow = dt.NewRow();

                        tempRow[0] = da[0];

                        tempRow[1] = da[1];

                        tempRow[2] = da[2];

                        tempRow[3] = da[3];

                        dt.Rows.Add(tempRow);

                        this.progressBar1.Value++;

                    }

                    ds.Tables.Add(dt);

                    // sda.Fill(ds);

                    this.dataGridView1.DataSource = ds.Tables[0];

     

     

    Note: If these codes were put in constructor or load event handler, the effect will not be seen.

     

    If you have difficulty in understanding c# language, please use the convert tool to have a translation: http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx.

     

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Tuesday, August 12, 2008 5:10 AM
  • Thank you so very much, Bruce. I will attempt to use that code. It looks straightforward and as if it should work. In the meantime I've temporarily solved my problem with a very simple solution.

    I created a second form for my project and placed a progressbar on the form. I then selected the "properties" for the project, set my primary form as the startup form, and designated the second form as the "Spalsh screen."

    It works like a charm. The second form with the progressbar stays visible until the dataset is filled, the dataset bound to the datagridview, then the splash screen closes.

    Thank you all for your advice!

     

    Tuesday, August 12, 2008 10:53 AM