Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Need to sum last column SATI


What I have tried:

while (rd.Read())
{
    usersNB.Add(new userNB
    {
        //my sql

        Vrijeme = rd.IsDBNull(rd.GetOrdinal("VRIJEME")) ? string.Empty : rd.GetString(rd.GetOrdinal("VRIJEME")),
        Osoba = rd.GetString("OSOBA"),
Sati = rd.IsDBNull(rd.GetOrdinal("SATI")) ? string.Empty : rd.GetInt32(rd.GetOrdinal("SATI")).ToString()



}
Posted
Comments
Maciej Los 22-Jan-24 11:20am    
What is rd?
Stylus STYLUS 23-Jan-24 1:31am    
Data reader
var rd = cmd.ExecuteReader();

1 solution

So, you might want to write something like this:
C#
long runningTotal = 0;
while (rd.Read())
{
  string sati = string.Empty;
  if (!rd.IsDBNull(rd.GetOrdinal("SATI"))
  {
    int satiVal = rd.GetInt32(rd.GetOrdinal("SATI"));
    runningTotal += satiVal;
    sati = satiVal.ToString();
  }
  string vrijeme = rd.IsDBNull(rd.GetOrdinal("VRIJEME")) ? string.Empty : rd.GetString(rd.GetOrdinal("VRIJEME"));
  usersNB.Add(new userNB{ Vrijeme = vrijeme, Osoba = rd.GetString("OSOBA"), sati });
}
All you're doing is keeping a running total as you step through the reader. The reason that runningTotal is long rather than int is just to cater for overflowing the maximum or minimum (underflow) value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900