Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have got problem with two loops. If I adding one its good, but with more it duplikates or more the values. I put this 3 Values {1/8, 1/8, 3/8} And want to get in sum 5/8. Because 1/8+1/8+3/8 = 5/8

I geting 3/8, 3/8, and 9/8

I want one Sum not three or more. Now after puts to exercise {1/8, 1/8, 3/8} i get:

enter image description here

Here is my code.

  else
        {
            query1.Query = String.Format(FullQuery, "Dzien", dateISO, DropDownList1.SelectedItem.Text);
            query1.ViewFields = "<FieldRef Name='Kontrakt' /><FieldRef Name='Procenty' />";
            query1.ViewFieldsOnly = true;

            SPListItemCollection listaDniPrzeznaczonychNaProjekt = witryna.Lists["Szczegoly"].GetItems(query1);



            // Gdy nie ma zaznaczonej osoby to wtedy wyświetlamy wszystkie daty urlopów 
            foreach (SPListItem olistaDniPrzeznaczonychNaProjekt in listaDniPrzeznaczonychNaProjekt)
            {

                e.Cell.Style.Add("background-color", "#F5BCA9");
                e.Cell.CssClass = "dni_swiateczne";
                e.Cell.Controls.Add(new LiteralControl(("<p style='color:blue; font-size:8px;'>" + olistaDniPrzeznaczonychNaProjekt["Kontrakt"].ToString() + "</p><p style='color:#5D198E;'>" + olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().Substring(3) + "</p>")));

                var query3 = new SPQuery();
                query3.Query = "<Where><Eq><FieldRef Name='Dzien' /><Value Type='DateTime'>" + e.Day.Date.ToShortDateString() + "</Value></Eq></Where>";
                SPListItemCollection itemColl = witryna.Lists["Szczegoly"].GetItems(query3);
                int sumaProcentow = 0;
                foreach (SPListItem item in itemColl)
                {
                    int index;

                    index = olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().IndexOf("#");
                    string procentKolejny = Convert.ToString(olistaDniPrzeznaczonychNaProjekt["Procenty"]).Substring(index + 1,1);

                    sumaProcentow += int.Parse(procentKolejny);
                }

                e.Cell.Controls.Add(new LiteralControl(("Suma = " + sumaProcentow.ToString() + "/8")));
                if (sumaProcentow > 8)
                {
                    e.Cell.Style.Add("background-color", "#FF0000");
                    e.Cell.CssClass = "dni_swiateczne";
                }
            }

            }

Please help.

share|improve this question
1  
Grzegorz, Can you please post the value for olistaDniPrzeznaczonychNaProjekt["Procenty"] –  Arsalan Adam Khatri Oct 23 '12 at 15:21
    
There is lookap field : "6;#1/8" so i geting read only 1/8 - thats why this code because i want only "1", and next add them. index = olistaDniPrzeznaczonychNaProjekt["Procenty"].ToString().IndexOf("#"); string procentKolejny = Convert.ToString(olistaDniPrzeznaczonychNaProjekt["Procenty"]).Substring(index + 1,1); –  Grzegorz Z Oct 23 '12 at 16:29
add comment

2 Answers 2

up vote 1 down vote accepted

Instead of olistaDniPrzeznaczonychNaProjekt["Procenty"] use item["Procenty"]

foreach (SPListItem item in itemColl)
{
   int index;

   index = item["Procenty"].ToString().IndexOf("#");
   string procentKolejny = Convert.ToString(item["Procenty"]).Substring(index + 1,1);

   sumaProcentow += int.Parse(procentKolejny);
}
share|improve this answer
add comment

Here's your issue. Say your outer for each loop iterates 3 times. For each iteration, you are querying for your number values and adding each of them to the "sum". Do you really want to add them all each time the outer loop iterates?

Look at the values in the top left red box on the 23rd.

1/8 -> suma = 3/8

1/8 -> suma = 3/8

3/8 -> suma = 9/8

Each is multiplied three times over because your loop is iterating three times.

1/8 + 1/8 + 1/8 = 3/8

3/8 + 3/8 + 3/8 = 9/8

You should think through your logic a little bit here.

share|improve this answer
    
I know, I wont only add one time. Only one, but i don't knowed how it done. –  Grzegorz Z Oct 23 '12 at 17:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.