Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have this piece of code here , basically i am doing a for loop as i retrieve records from database( using entity framework ) but when i want to compare using if statement theres an error :

       IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID);

        for(int i = 0 ; i <lstQuestion.Count()-1 ; i++)
        {
             .... //code here

            if(lstQuestion[i].QuestionNo == lstQuestion[i++].QuestionNo) // error at i++
            {
                tb.Text = lstQuestion[i++].QuestionContent;
                sp1.Children.Add(tb);
            }

i tried

lstQuestion.Count() instead of lstQuestion.Count()-1;

also doesn't work .

QuestionNo is a column in my database table .

Full ERROR :

enter image description here

When i remove the whole if statement , it works fine .

share|improve this question
    
InnerException says {"Specified Visual is already a child of another Visual or the root of a CompositionTarget."} –  user2376998 Jul 17 '13 at 3:37
add comment

5 Answers

you're incrementing i three times. Try this:

for(int i = 0 ; i <lstQuestion.Count(); i++)
        {
             .... //code here

            if(lstQuestion[i].QuestionNo == 1stQuestion[i+1].QuestionNo) // error at i++
            {
                tb.Text = lstQuestion[i+1].QuestionContent;
                sp1.Children.Add(tb);
            }
share|improve this answer
    
tried , i have updated the error –  user2376998 Jul 17 '13 at 3:41
    
it sounds like it's the line sp1.Children.Add(tb); that's causing you trouble. I'm not entirely sure what you're trying to do there without seeing it in context. tb is apparently already a child of something else. –  Bodacious Jul 17 '13 at 4:00
    
i didn't paste the full code because i tot it was not related , sp1 is the name of the stackpanel that i am adding the tb to. –  user2376998 Jul 17 '13 at 4:00
    
i removed sp1.children.add(tb) and tb.text line but i keep the if statement , the error is still –  user2376998 Jul 17 '13 at 4:01
    
Is it the exact same error? –  Bodacious Jul 17 '13 at 4:06
show 2 more comments

you have a number 1 in the == 1stQuestion[i] instead of the lowercase L like in the ohter lstQuestion[i] references.

share|improve this answer
    
typo , i changed it already –  user2376998 Jul 17 '13 at 3:38
add comment

the above answers regarding (i++) are correct. also, since collections use zero based index, your should use (Count() - 2) --- // (Count() -2) + 1 is your last item in the collection

share|improve this answer
add comment

This should solve your problem with if statement.

    for(int i = 0 ; i <lstQuestion.Count()-1; i++)
    {
         .... //code here

        if(lstQuestion[i].QuestionNo == 1stQuestion[i+1].QuestionNo) 
        {
            tb.Text = lstQuestion[i+1].QuestionContent;
            sp1.Children.Add(tb);
        }

But i think you are getting error on this line

                      sp1.Children.Add(tb);
share|improve this answer
add comment

Try using ++i instead of i++.

IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID);

        for(int i = 0 ; i <lstQuestion.Count()-1 ; i++)
        {
             .... //code here

            if(lstQuestion[i].QuestionNo == lstQuestion[++i].QuestionNo) // error at i++
            {
                tb.Text = lstQuestion[i].QuestionContent;
                sp1.Children.Add(tb);
            }
share|improve this answer
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.