- I seem to be able to create a new student, & to be able to add them to the BindingSource. I know this because the show up in the
I seem to be able to create a new student, & to be able to add them to the BindingSource. I know this because the show up in the DataGridView. But, their id value is always 0. And, I need for the id value to be unique.
DataGridView. But, their id value is always 0. And, I need for the idWhen I save the changes made in the BindingSource to the underlying db file the new students are not saved. If I edit the credits of a student, these edits will save. But, the newly created students will not.
value to be unique.
- When I save the changes made in the BindingSource to the underlying db file the new students are not saved. If I edit the credits of a student, these edits will save. But, the newly created students will not.
// this would be global
private List<student> studentList = new List<student>();
public void AddStudent(int id, string firstName, string lastName, int currentCredits, int totalCredits)
{
var _student = new student
{
Id = id,
StudentFirstName = char.ToUpper(firstName[0]) + firstName.Substring(1),
StudentLastName = char.ToUpper(lastName[0]) + lastName.Substring(1),
CurrentCredits = currentCredits,
TotalCredits = totalCredits
};
studentList.Add(_student);
}
public void CommitNewStudents()
{
// get the new students that have not been committed yet.
var numberOfnewStudents = studentList.Count;
if(numberOfnewStudents > 0)
{
//get the current max Id from the database.
var maxDbId = student.NewId();
for(int x =0; x < numberOfnewStudents; x++)
{
// since the id is max + 1, then we can get the count of the new students, and add 1 to the maxId.
studentList[x].Id = maxDbId + x;
}
//now, add them to the entity and save the the changes.
db.students.AddRange(studentList);
db.SaveChanges();
}
}