0
votes

What it does:

Reads the values in a datagridview into the database ACTB row by row.


To be clear, this exact same code works on my other program without issue, I don't know why my current program is throwing errors every time I run the background worker.

I receive the error: 2500 is not a valid value for 'Value' (progressbar.value) 'Value' must be between minimum and maximum.

Here is my code.

Do Work:

    int MaxSchema = 0;
    private void Transfer_Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(Con))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    conn.Open();
                    MaxSchema = DGVExcel.Rows.Count;
                    for (int s = 0; s < DGVExcel.Rows.Count; s++) 
                    {
                    Transfer_Worker.ReportProgress(s);
                    }
                }
            }
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("Import error: " + ex);
        }
        Transfer_Worker.ReportProgress(100);
    }

Progress Changed:

 private void Transfer_Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        pbar.Value = (e.ProgressPercentage * 100) / MaxSchema;
    }

Now, commenting out my code in progress changed seems to make the process run as expected, even the progressbar's values get updated. I don't know why this is, if the same block of code works elsewhere.

I have not modified the progressbar in anyway, so it should still have default properties.

Could it be that it is because I report progress on my dowork function? Correct me if this is something I need to change. First of all, I assume that I am not permitted to update the UI with the Work method of Background workers, so it should throw an error but it doesn't.

EDIT: It appears that the error is caused by the line

  Transfer_Worker.ReportProgress(100);

Changing the value from 100 (Presumed maximum) to MaxSchema (Actual value of rows.Count), the progress bar was able to correctly determine the end point.

1
in (e.ProgressPercentage * 100) / MaxSchema, ProgressPercentage will e as large as 100 and MaxSchema = 3, so the value will be very large, but the maxvalue is 100, so exceeded.Lei Yang
"I have no idea where this number is coming from because the sheet data I am trying to insert only contains a maximum of 3 rows plus a header" -- did you actually verify that assumption by debugging the code and checking to see what DGVExcel.Rows.Count returns? I'll also point out that (100 * 100) / 3 (where 3 is what you claim MaxSchema will be set to) is 3333, well above the default MaxValue for a ProgressBar. In any case, until you fix your question so it includes a good minimal reproducible example that reliably reproduces the problem, it's not possible to say for sure what your bug is.Peter Duniho
(Just to be clear, (100 * 100) / MaxSchema comes from your program statement Transfer_Worker.ReportProgress(100); at the end of the DoWork event handler.)Peter Duniho
I am glad you were able to fix the problem. I don't feel it would be appropriate for me to post an answer, as frankly I still don't know how (100 * 100) / 3 leads to 25500. If you feel you can clean up the question, so it includes a good minimal reproducible example, and you can post an answer yourself (which you can self-accept) that fully and logically explains the error and how to fix it, you should consider doing that.Peter Duniho
(Dealing with more rows would prevent the problem, because you're dividing by MaxSchema. With enough rows, the result of the calculation is small, and with 2700000 rows, the result is 0, well below the default maximum value :). To get correct results, you should probably be passing MaxSchema instead of 100 at that line.)Peter Duniho

1 Answers

0
votes

Assigning a default value (100) to the TransferWorker's ReportProgress property caused the progress bar value to bloat past 100% when the value of MaxSchema was much smaller than that. (100*100)/4 = the reported value of 2500.

Change the value of report progress to match the actual row count. In this case, the correct line would be:

 TransferWorker.ReportProgress(MaxSchema);

Regarding the second issue of it working in the past with a significantly larger collection of rows, as stated by Peter:

 Dealing with more rows would prevent the problem, because you're dividing by MaxSchema. With enough rows, the result of the calculation is small, and with 2700000 rows, the result is 0, well below the default maximum value.`"