title: C# Entity Framework issues
body: <p>I am having a bit of an issue with my program now that I transferred it over to the Entity Framework context.  I keep getting an index out of range argument which means that my program is not counting the number of classes or keeping track of the total credit hours I am trying putting into my list box.  The program itself is supposed to take items selected from the combobox put them in the list box and write back to the database when the button is clicked.  I will be adding my code (at least what I think is relevant) down below.  Thanks and have a great day.</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
          {
          try
          {
              context = new CourseContext();
              context.Courses.Load();
              courseBindingSource.DataSource = context.Courses.Local.ToBindingList();
              courseComboBox.DataSource = courseBindingSource.DataSource;
              courseComboBox.DisplayMember = "CourseNumber";
              courseComboBox.ValueMember = "CourseNumber";
  
              display = true;
              registeredCourseList.View = View.List;
  
  
              selectedContext = new SelectedCourseContext();
              selectedContext.SelectedCourses.Load();
              selectedCourseBindingSource.DataSource = selectedContext.SelectedCourses.Local.ToBindingList();
  
              for (int i = 0; i &lt; context.Courses.Local.Count; i++)
              {
                  string courseNumber = selectedContext.SelectedCourses.Local[i].CourseNumber;
              }
                  statusMessageText.Text = $"{courses.Count} Courses available\n" +
                  "Select a course from comboBox above";
          }
          catch(SqlException ex)
          {
              MessageBox.Show($"Error reading data: {ex.Message}");
          }
      }
  
  private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
          if (display)
          {
              Course chosenCourse = context.Courses.Local[courseComboBox.SelectedIndex];
              registeredCourseList.Items.Add(chosenCourse.ToString());
  
              //If the course is not registered for
              if (!courses[courseComboBox.SelectedIndex].IsRegisteredFor)
              {
                  //If the creditTotal + the course credits is not less than or equal too 9
                  if (creditTotal + chosenCourse.Credits &lt;= 9)
                  {
                      //The course is registered for
                      statusMessageText.ForeColor = System.Drawing.Color.Black;
                      courses[courseComboBox.SelectedIndex].IsRegisteredFor = true;
                      registeredCourseList.Items.Add(courseComboBox.SelectedText);
  
                      //The statusMessageLabel reads the course name and finishes the statement
                      statusMessageText.Text = $"{chosenCourse.CourseNumber} selected";
  
                      //The credits are added to the total
                      creditTotal += chosenCourse.Credits;
                      totalCreditValueLabel.Text = $"{creditTotal}";
                  }
                  else
                  {
                      /*If you have 9 credit hours or choose a course with more
                       * than 9 the program will not allow it and displays a message
                       */
                      statusMessageText.ForeColor = System.Drawing.Color.Red;
                      statusMessageText.Text = "Cannot register for more than 9 credits";
                  }
              }
              else
              {
                  /*If you have already selected a course the statusMessageText will
                   * Tell the user they have already selected that course
                   */
                  statusMessageText.ForeColor = System.Drawing.Color.Red;
                  statusMessageText.Text = $"{chosenCourse.CourseTitle} already selected";
              }
  
              SelectedCourse c = new SelectedCourse();
              c.UserID = userNameTextBox.Text;
              c.CourseNumber = chosenCourse.CourseNumber;
              selectedContext.SelectedCourses.Local.Add(c);
          }
      }
  </code></pre>
  
  <p>My Program</p>
  
  <p><img src="https://i.stack.imgur.com/JlC4R.png" alt="enter image"></p>
  
  <p>Error:</p>
  
  <p><img src="https://i.stack.imgur.com/TD6t0.png" alt="enter image"></p>