0

I have the following C# Code:

for (int i = 0; i < dgItems.Rows.Count - 1; i++)
                    {
                        SqlCommand cmd = new SqlCommand(@"INSERT INTO TBL_OrderQty (SipStyle,SipColor,SipSize,[SipQty(-%5)],SipQty,[SipQty(+%5)]) VALUES('" + dgItems.Rows[i].Cells[0].Value + "','" + dgItems.Rows[i].Cells[1].Value + "','" + dgItems.Rows[i].Cells[2].Value + "','" + dgItems.Rows[i].Cells[3].Value + "','" + dgItems.Rows[i].Cells[4].Value + "','" + dgItems.Rows[i].Cells[5].Value + "')", con);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        progressBar1.Value = i;
                        LB_ProgText.Text = "Progress :%" + (i / 100) * dgItems.Rows.Count;
                        LB_ProgText.Invalidate();
                        LB_ProgText.Update();
                    }

I'm trying to show the progress percentage of the whole operation, the progress bar is working just fine but I couldn't get the percentage right; can you please help me.

Mark
  • 75
  • 6

3 Answers3

2

First of all, this code is vulnerable to SQL injections. Please use Parametized Queries.

Secondly, this should not be a question. If it takes long enough to consider progress reporting, soemthing is fundamentally flawed with the process. There is a limit to how much data a human can process. And retreiving more then that data from the DB to do filtering in the UI, is a classical beginners mistake. Right next to SQL Injection vulnerability. Do as much sorting, filtering and pagination in the query as possible. If you are on mobile, consider a Distributed Design.

As for the actuall question:

You can only report progress between distinct lines of code. Some modern classes do support Events for progress reporting, but the DB classes are about 2 decades to old for that. You basically got these options:

  • find a class taht supports this
  • make a loop retreiving each row seperately. However this is a no-go in itself. Always retreive as much in 1 go as possible
  • Use a enumerator approach to retrieve the data. However this will usually not give you a count of the total amount until you reached the end.
  • realize this is way more trouble then it is worth and learn to live without it. The Windows Update people had to learn to live without that level of reporting and those guys had resources.
Christopher
  • 9,634
  • 2
  • 17
  • 31
2

You can calculate one hundred percent and then just delete current iteration:

var oneHundred = Rows.Count - 1;
for (int i = 0; i < dgItems.Rows.Count - 1; i++)
{
     SqlCommand cmd = new SqlCommand(/*The code is omitted for the brevity*/);
     con.Open();
     cmd.ExecuteNonQuery();
     con.Close();
     var currentPercent = ((100 * i) / oneHundred )
     progressBar1.Value = currentPercent ;
     LB_ProgText.Text = "Progress :%" + currentPercent;
     LB_ProgText.Invalidate();
     LB_ProgText.Update();
 }

In addition, you need to use parameterized SQL to avoid SQL injections.

UPDATE:

As we have sign < in i < dgItems.Rows.Count - 1, then it means, that i never become equal to dgItems.Rows.Count - 1. So we can slightly modify our condition to:

i < dgItems.Rows.Count

The whole code will looks like this:

var rowsCount = 8001;
var oneHundred = rowsCount - 1;
for (int i = 0; i < rowsCount; i++)
{
    var currentPercent = ((100 * i) / oneHundred);
    if (currentPercent > 97)
    {
        Console.WriteLine(currentPercent);
    }   
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • And that code still does not report anywhere near the level he wants. He wants/needs progress reporting each row processed by ExecuteNonQuery(). – Christopher May 31 '20 at 18:14
  • @StepUp Thank you this actually worked, but I only got the number 99 when the operation is over. – Mark May 31 '20 at 18:36
-1

Use (i / (float)dgItems.Rows.Count) * 100. You can also format it with this: Math.Round((i / (float)dgItems.Rows.Count) * 100, n) where n is the number of decimals

Raphtaliyah
  • 207
  • 1
  • 6
  • That does not help if he has no distinct lines to report *between*. Wich is one of the biggest issues with progress reporting. – Christopher May 31 '20 at 17:47