Solved

Problems with foreach

Posted on 2006-07-03
7
354 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now