• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

Output the subtotal in while loop

Hi Experts,

What is the best way to output the subtotal in while (reader.read()) in C#? I have the while loop to read records and if the index '0' or '1' changed; I need to write out the subtotal column for index '4, 5, and 6'.

while (reader.Read())
{
string row = "<tr>";
for (int index = 0; index < reader.FieldCount; index++)
{                    
      switch (index)
      {
      case 0:                            
      case 1:
      case 2:
      case 3:      
            row += "<td>";
            row += reader.GetString(index);
            break;
      case 4:
      case 5:
      case 6:
            row += "<td>";
            row += reader.GetInt32(index).ToString();
            break;                    
      default:                          
            break;
      }
      row += "</td>";
}
row += "</tr>\n";
MyReport.Text += row;
}

Thanks.
0
alexcryout
Asked:
alexcryout
  • 2
1 Solution
 
Jaime OlivaresCommented:
You need to manage individually case 0 and case 1 and store the result in two local variables, let's say last0 and last1
in the same cases, compare the new colum 0 value with the last one. if it is different, turn on a flag variable so you can write a total after you print the whole row, let's call it mustTotalize
0
 
Craig WagnerSoftware ArchitectCommented:
Unless I'm completely misunderstanding the problem I don't understand why the for() loop exists inside your while() loop. It seems like all you need to do is compare the values from columns 0 and 1 on each iteration of the while() loop and, if either of them are different, write out a row. The following code hasn't been tested but it should cover the basics.
if( reader.Read() )
{
    string oldValue0 = reader[0].ToString();
    string oldValue1 = reader[1].ToString();

    int total4 = (int)reader[4];
    int total5 = (int)reader[5];
    int total6 = (int)reader[6];

    while( reader.Read() )
    {
        if( oldValue0 != reader[0].ToString() || oldValue1 != reader[1].ToString() )
        {
            string row = "<tr><td>";
            row += oldValue0;
            row += "</td></td>";
            row += oldValue1;
            row += "</td><td>";
            row += total4.ToString();
            row += "</td><td>";
            row += total5.ToString();
            row += "</td><td>";
            row += total6.ToString();
            row += "</td></tr>";

            MyReport.Text += row;

            oldValue0 = reader[0].ToString();
            oldValue1 = reader[1].ToString();

            total4 = (int)reader[4];
            total5 = (int)reader[5];
            total6 = (int)reader[6];
        }
        else
        {
            total4 += (int)reader[4];
            total5 += (int)reader[5];
            total6 += (int)reader[6];
        }
    }

    string lastRow = "<tr><td>";
    lastRow += oldValue0;
    lastRow += "</td></td>";
    lastRow += oldValue1;
    lastRow += "</td><td>";
    lastRow += total4.ToString();
    lastRow += "</td><td>";
    lastRow += total5.ToString();
    lastRow += "</td><td>";
    lastRow += total6.ToString();
    lastRow += "</td></tr>";

    MyReport.Text += lastRow;
}

Open in new window

0
 
alexcryoutAuthor Commented:
Thanks CraigWagner. It works.
0
 
alexcryoutAuthor Commented:
Hi CraigWagner,

I found error in the code:

if( reader.Read() )  <----- don't have data to assign to these variables yet.
{
    string oldValue0 = reader[0].ToString();
    string oldValue1 = reader[1].ToString();

    int total4 = (int)reader[4];
    int total5 = (int)reader[5];
    int total6 = (int)reader[6];

    while( reader.Read() )
    {....
    }
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now