Solved

Problems with foreach

Posted on 2006-07-03
7
360 Views
Last Modified: 2010-08-05
Hello. I get some problems when trying to sum rows in a table. The first column I try to sum works, but the second column don't work. Why??

        if (list.Count != 0)
        {
            StrSqlTillegg += " [TilleggIndex] = " + list[0] + "";
            for (int i = 1; i < list.Count; i++)
            {

                StrSqlTillegg += " OR [TilleggIndex] = " + list[i] + "";
            }

            string Strsql2 = "SELECT [tillegg], [" + Alt1 + "], [" + Alt2 + "]  FROM [GrDynTilleggTilWeb] WHERE [Hyttenavn] = '" + Hyttenavn + "' AND " + StrSqlTillegg + "";
            Da = new OleDbDataAdapter(Strsql2, Conn);
            Ds.Tables.Add("tablename3");
            Da.Fill(Ds, "tablename3");
            Dt = Ds.Tables["tablename3"];
            GridViewDyn.DataSource = Dt;
            this.GridViewDyn.DataBind();          

            //More dynamical sums
            //Sum dyn alt1
            foreach (DataRow dr_ in Dt.Rows)
            {
                DecSumMoreDynamicalAlt1 += Convert.ToDecimal(dr_[Alt1]);
            }
            LblSumMoreDynamicalAlt1.Text = DecSumMoreDynamicalAlt1.ToString();

            //Sum dyn alt2 <--------------------------------------------------------------- this one gives me wrong sum.
            foreach (DataRow dr_ in Dt.Rows)
            {
                DecSumMoreDynamicalAlt2 += Convert.ToDecimal(dr_[Alt2]);
            }
            LblSumMoreDynamicalAlt2.Text = DecSumMoreDynamicalAlt2.ToString();                        
        }
    }
0
Comment
Question by:m-jansen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17030173
have you tried using Sum() in the sql itself?
0
 

Author Comment

by:m-jansen
ID: 17030193
>have you tried using Sum() in the sql itself?
No. Is it just to replace SELECT with SUM?
0
 
LVL 23

Expert Comment

by:apresto
ID: 17030198
no you include the sum in the select and put the field you want to sum in the ()

i.e

SELECT Sum(FieldtoSum), Field2, Field3 FROM MyTable...
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:m-jansen
ID: 17030223
apresto: I'll try that.

I would be glad if some could tell what I have done wrong with my second foreach loop too. Is there an iterator that's not rested or something?
0
 
LVL 23

Accepted Solution

by:
apresto earned 250 total points
ID: 17030231
where is this declared:

DecSumMoreDynamicalAlt2

try setting it to 0 before the loop - when you say its, wrong, how, is it too big, too small, a negative number?
0
 

Author Comment

by:m-jansen
ID: 17030297
DecSumMoreDynamicalAlt2 is declared in the top of my class above the constructor. And it is too big.

When setting it
            //Sum dyn alt2
            DecSumMoreDynamicalAlt2 = 0;
            foreach (DataRow dr_ in Dt.Rows)
            {
                DecSumMoreDynamicalAlt2 += Convert.ToDecimal(dr_[Alt2]);
            }
            LblSumMoreDynamicalAlt2.Text = DecSumMoreDynamicalAlt2.ToString();      

I got the right sum! So we found out that I was messing with DecSumMoreDynamicalAlt2..... Great. Thanks!
0
 
LVL 23

Expert Comment

by:apresto
ID: 17030307
No problem, glad to help,

However if you can execute the sum in the SQL, thats what its there for :o)

Ciao for noe

Apresto
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question