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.
(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 YangDGVExcel.Rows.Count
returns? I'll also point out that(100 * 100) / 3
(where3
is what you claimMaxSchema
will be set to) is3333
, well above the defaultMaxValue
for aProgressBar
. 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(100 * 100) / MaxSchema
comes from your program statementTransfer_Worker.ReportProgress(100);
at the end of theDoWork
event handler.) – Peter Duniho(100 * 100) / 3
leads to25500
. 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 DunihoMaxSchema
. With enough rows, the result of the calculation is small, and with2700000
rows, the result is0
, well below the default maximum value :). To get correct results, you should probably be passingMaxSchema
instead of100
at that line.) – Peter Duniho