Solved

Problems with foreach

Posted on 2006-07-03
7
357 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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