У меня возникла небольшая проблема с моей программой, когда я перевел ее в контекст Entity Framework.Я продолжаю получать индекс вне аргумента диапазона, который означает, что моя программа не считает количество классов или отслеживает общее количество кредитных часов, которые я пытаюсь поместить в свой список.Предполагается, что сама программа берет выбранные в выпадающем списке элементы, помещает их в список и записывает обратно в базу данных при нажатии кнопки.Я буду добавлять свой код (по крайней мере, то, что я считаю уместным) ниже.Спасибо и хорошего дня.
Мой код:
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 < 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 <= 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);
}
}
Моя программа
![enter image](https://i.stack.imgur.com/JlC4R.png)
Ошибка:
![enter image](https://i.stack.imgur.com/TD6t0.png)