Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problems with foreach

Posted on 2006-07-03
7
Medium Priority
?
362 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
Industry Leaders: 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

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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