2

I'm developing a .NET application, and I have a problem. I try to populate a listbox from a MySQL database with Entity Framework. Here is my code:

public MainForm()
        {
            InitializeComponent();
            try
            {
                hospitaldbEntities context = new hospitaldbEntities();
                PatientlistBoxId.DataSource = context.patients;
                PatientlistBoxId.DisplayMember = "FirstName";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

I attach a picture from the Solution Explorer: enter image description here

And finally here is my patient class:

public partial class patient
    {
        public patient()
        {
            this.examinations = new HashSet<examination>();
            this.diseases = new HashSet<disease>();
            this.drugsensitivities = new HashSet<drugsensitivity>();
        }

        public int PatientID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
        public System.DateTime DateOfCreate { get; set; }
        public string Category { get; set; }
        public int CNP { get; set; }

        public virtual ICollection<examination> examinations { get; set; }
        public virtual ICollection<disease> diseases { get; set; }
        public virtual ICollection<drugsensitivity> drugsensitivities { get; set; }
    }

Thank you for the help in advance.

EDIT

When I try to run the application, it throws an error: enter image description here

3
  • what's happening? Some error? Commented Apr 30, 2015 at 22:30
  • I added some more details. Commented Apr 30, 2015 at 22:37
  • The exception tells you exactly what to to. Commented May 1, 2015 at 10:34

1 Answer 1

1

You must at least use:

PatientlistBoxId.DataSource = context.patients.ToList();

Consider using a BindingList<T>

BTW:

Consider not using an Entity type as ItemType for the ListBox.

Consider using a using context to dispose your context, that is handle more clearly the scope of your context.

========== ==========

A little bit more details

//this class allows to separate the winform (equivalent to a view) from 
//the data layer, you can see it as a ViewModel
public class FormNamePatient {
    public Int32 Id { get; set;}
    public String FirstName { get; set;}
    public String LastName { get; set;}
}

private BindingList<FormNamePatient> _patients;
try {
   //the using guarantees the disposing of the resources 
   //(avoiding memory link and so on)
   using ( hospitaldbEntities context = new hospitaldbEntities() ) {
       _patients = new BindindList<FormNamePatient>(
           context.patients.Select(
              x => new FormNamePatient {
                  Id = x.Id,
                  FirstName = x.FirstName,
                  LastName = x.LastName,
              }).ToList() // this materialize the list
                          // in other (bad ?) words, this allows the list
                          //to live out of the context (<=> using)
       );       
   }
   PatientlistBoxId.DataSource ) = _patients;
   //now if you alter (not erase, but edit, change...) _patients you 
   //update the control
   PatientlistBoxId.DisplayMember = "FirstName";
} catch (Exception ex) {
  Console.WriteLine(ex);
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.