Skip to main content
deleted 1171 characters in body
Source Link
delete
delete
 public Form1() { InitializeComponent(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; LoadUsersToComboBox(); } PersonRepository peopleRepo = new PersonRepository(); private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } private void button2_Click(object sender, EventArgs e) {   CreateNewPerson();  LoadUsersToComboBox(); } private void CreateNewPerson() { if (textBox2.Text != String.Empty) { Person user = new Person() { Name = textBox2.Text }; peopleRepo.Add(user);   peopleRepo.Save(); } } 

This method will load a comboBox with value only on the FIRST time, but not on subsequent attempts:

Why does the first one only load the first time?

Thanks

Edit:

Here is the code to the PeopleRepository class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SQLite_Testing_Grounds { public class PersonRepository { private ScansEntities3 db = new ScansEntities3(); public IQueryable<Person> FindAllPeople() { return db.People; } public Person FindPerson(int id) { return db.People.SingleOrDefault(p => p.ID == id); } public void Add(Person person) { db.AddToPeople(person); } public void Delete(Person person) { foreach (var department in person.Departments) { db.DeleteObject(department); } db.DeleteObject(person); } public void Save() { db.SaveChanges(); } } } 
 public Form1() { InitializeComponent(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; LoadUsersToComboBox(); } PersonRepository peopleRepo = new PersonRepository(); private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } private void button2_Click(object sender, EventArgs e) {   CreateNewPerson();  LoadUsersToComboBox(); } private void CreateNewPerson() { if (textBox2.Text != String.Empty) { Person user = new Person() { Name = textBox2.Text }; peopleRepo.Add(user);   peopleRepo.Save(); } } 

This method will load a comboBox with value on the FIRST time, but not on subsequent attempts:

Why does the first one only load the first time?

Thanks

Edit:

Here is the code to the PeopleRepository class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SQLite_Testing_Grounds { public class PersonRepository { private ScansEntities3 db = new ScansEntities3(); public IQueryable<Person> FindAllPeople() { return db.People; } public Person FindPerson(int id) { return db.People.SingleOrDefault(p => p.ID == id); } public void Add(Person person) { db.AddToPeople(person); } public void Delete(Person person) { foreach (var department in person.Departments) { db.DeleteObject(department); } db.DeleteObject(person); } public void Save() { db.SaveChanges(); } } } 
 public Form1() { InitializeComponent(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; LoadUsersToComboBox(); } PersonRepository peopleRepo = new PersonRepository(); private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } private void button2_Click(object sender, EventArgs e) { LoadUsersToComboBox(); } 

This method will load a comboBox with value only on the FIRST time, but not on subsequent attempts:

Why does the first one only load the first time?

Here is the code to the PeopleRepository class:

namespace SQLite_Testing_Grounds { public class PersonRepository { private ScansEntities3 db = new ScansEntities3(); public IQueryable<Person> FindAllPeople() { return db.People; } } } 
added 1943 characters in body; added 168 characters in body
Source Link
delete
delete
 public Form1() { InitializeComponent(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; LoadUsersToComboBox(); } PersonRepository peopleRepo = new PersonRepository(); private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } private void button2_Click(object sender, EventArgs e) { CreateNewPerson(); LoadUsersToComboBox(); } private void CreateNewPerson() { if (textBox2.Text != String.Empty) { Person user = new Person() { Name = textBox2.Text }; peopleRepo.Add(user); peopleRepo.Save(); } } 

Thanks

Edit:

Here is the code to the PeopleRepository class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SQLite_Testing_Grounds { public class PersonRepository { private ScansEntities3 db = new ScansEntities3(); public IQueryable<Person> FindAllPeople() { return db.People; } public Person FindPerson(int id) { return db.People.SingleOrDefault(p => p.ID == id); } public void Add(Person person) { db.AddToPeople(person); } public void Delete(Person person) { foreach (var department in person.Departments) { db.DeleteObject(department); } db.DeleteObject(person); } public void Save() { db.SaveChanges(); } } } 

Thanks

 public Form1() { InitializeComponent(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; LoadUsersToComboBox(); } PersonRepository peopleRepo = new PersonRepository(); private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } private void button2_Click(object sender, EventArgs e) { CreateNewPerson(); LoadUsersToComboBox(); } private void CreateNewPerson() { if (textBox2.Text != String.Empty) { Person user = new Person() { Name = textBox2.Text }; peopleRepo.Add(user); peopleRepo.Save(); } } 

Thanks

Edit:

Here is the code to the PeopleRepository class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SQLite_Testing_Grounds { public class PersonRepository { private ScansEntities3 db = new ScansEntities3(); public IQueryable<Person> FindAllPeople() { return db.People; } public Person FindPerson(int id) { return db.People.SingleOrDefault(p => p.ID == id); } public void Add(Person person) { db.AddToPeople(person); } public void Delete(Person person) { foreach (var department in person.Departments) { db.DeleteObject(department); } db.DeleteObject(person); } public void Save() { db.SaveChanges(); } } } 
Source Link
delete
delete

Why does this work and this doesn't?

This method will load a comboBox with value on the FIRST time, but not on subsequent attempts:

 private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople(); /*Return IQueryable<Person>*/ } 

And this loads every time I call LoadUsersToComboBox():

 private void LoadUsersToComboBox() { comboBox1.DataSource = peopleRepo.FindAllPeople().ToList(); } 

Why does the first one only load the first time?

Thanks