title: C# for loop to display all records
body: <p>I am doing this in wpf c# , I am trying to populate some records from a database.  There are 21 records.</p>
  
  <p>Here is how my code looks like:</p>
  
  <pre><code>private void PopulateQuestion(int activityID, int taskID)
  {
      IList&lt;ModelSQL.question&gt; lstQuestion = qn.GetRecords(taskID, activityID);
  
      for( int i= 0 ; i&lt;=lstQuestion.Count()-1; i++)
      {
          TextBlock tb = new TextBlock();
          tb.FontSize = 19;
          tb.FontWeight = FontWeights.Bold;
          tb.Text = lstQuestion[i].QuestionContent;
          tb.TextWrapping = TextWrapping.WrapWithOverflow;
          wrapPanel1.Children.Add(tb);
  
          TextBox tbox = new TextBox();
          if (lstQuestion[i].Answer.Trim().Length &gt; 0)
          {
              tbox.FontSize = 19;
              tbox.Width = 100;
              tbox.Height = 40;
              tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);
              tbox.Focusable = false; // Disallow user to input anything into it.
              wrapPanel1.Children.Add(tbox);
          }
          answers.Add(lstQuestion[i].Answer);
  
          if (lstQuestion[i].QuestionNo != lstQuestion[i + 1].QuestionNo)
          {
              StackPanel sp = new StackPanel();
              sp.Width = 1010;
            //  wrapPanel1.Children.Add(sp);
              Label spacing = new Label();
              spacing.Width = 1038;
              spacing.Content = "";
              wrapPanel1.Children.Add(spacing);
          }
  
      } // end of for each loop.
  }
  </code></pre>
  
  <p>I don't know what is related and what is not, so I just post the part where the for loop lies . the <code>lstQuestion.Count()</code> has 21 counts which is 21 records from database, however I got this error: </p>
  
  <blockquote>
    <p>{"Index was out of range. Must be non-negative and less than the size
    of the collection.\r\nParameter name: index"}</p>
  </blockquote>
  
  <p>So, I suppose something got to do with the for loop where <strong>i &lt;=lstQuestion.Count()</strong> .</p>
  
  <p>I tried to put <strong>i&lt;=lstQuestion.Count()-2</strong> ,</p>
  
  <p>it works but it doesn't display the last 2 records, it displays 19 records instead of 21.</p>
  
  <p>How do I change the for loop such that it displays all 21 records?</p>
  
  <p>I can't use foreach loop because I need to find the next loop value when I am in my current loop.</p>