Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problems with foreach

Posted on 2006-07-03
7
Medium Priority
?
364 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

971 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