0

I have this table structure

patients:

PK_Patient

PatientID

PatientName

items:

PK_Item

ItemID

ItemDesc

enrolleditems:

PK_EnrollMeds

FK_User_Add

DateTimeAdded

FK_Item

I have a form which has the user included on the form, and datagridview which displays the items enrolled to patient (binded to enrolleditems but items datamember is only displayed on grid), then add and delete button.

The entry on the screenshot is manually added on the database ([MED0001] enrolleditems table) for sample purposes.

EnrollMed Form

On the add button will open another forms which will load all the items and a checkbox to select which item to be added, then a select button which copy the selected datarows to datatable.

Item Selection Form


I have the below code for the Add button on Enroll Medication form

    M3dEntities m3d = new M3dEntities();
    enrollmeds _enrollmeds = new enrollmeds();
    EnrollMedSelectionFrm enrollselectfrm;
    public DataTable SelectedItems { get; set; }

    private void AddBtn_Click(object sender, EventArgs e)
    {
        enrollselectfrm = new EnrollMedSelectionFrm();

        var pxdetails = (from adm in m3d.admission
                        join pxDC in m3d.datacenter
                        on adm.FK_DC_Patient equals pxDC.PK_Datacenter
                        where adm.admissionNo == SelectedAdmNo
                        select new 
                        {
                            adm, 
                            pxDC
                        }).FirstOrDefault();

        if (enrollselectfrm.ShowDialog() == DialogResult.OK)
        {
            if (SelectedItems == null)
            {
                enrollmedsBindingSource.Clear();
            }
            else
            {
                enrollmedsBindingSource.Clear();

                foreach (DataRow dr in SelectedItems.Rows)
                {
                    _enrollmeds = new enrollmeds();
                    _enrollmeds.items = new items();

                    _enrollmeds.FK_DC_Patient = pxdetails.pxDC.PK_Datacenter;
                    _enrollmeds.FK_DC_userAdd = mainfrm.PK_DC_UserLoggedIn;
                    var svrDT = ((IObjectContextAdapter)m3d).ObjectContext.CreateQuery<DateTime>("CurrentDateTime() ");
                    DateTime currdatetime = svrDT.AsEnumerable().First();
                    _enrollmeds.AddDateTime = currdatetime;
                    _enrollmeds.FK_Admission = pxdetails.adm.PK_Admission;

                    int pkItems = int.Parse(dr.Field<string>("PK_Items").ToString());
                    var itemdtls = (from i in m3d.items
                                    where i.PK_Items == pkItems
                                    select i).FirstOrDefault();

                    _enrollmeds.FK_Items = pkItems;

                    _enrollmeds.items.ItemID = itemdtls.ItemID;
                    _enrollmeds.items.ItemDesc = itemdtls.ItemDesc;
                    _enrollmeds.items.GenericName = itemdtls.GenericName;
                    _enrollmeds.items.ItemGroup = itemdtls.ItemGroup;

                    enrollmedsBindingSource.Add(_enrollmeds);

                }
            }
        }
    }

My problem with this is only the first selected item on the itemselection form. I want to display all the items selected on the itemselection form to the enrollmedsDataGridView of enrollmedicines form.

Thank You in advance..

Henry
  • 155
  • 12
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) - I don't see anything that initializes `enrollmeds.items`. – Eugene Podskal Jun 26 '16 at 11:23
  • How do i initialize the enrollmeds.items without creating new instance? I tried this: enrollmeds.items.add(itemdtls) but nothing happens. No add and delete. :( – Henry Jun 26 '16 at 12:19
  • Probably by `enrollmeds.items = new List()`? – Eugene Podskal Jun 26 '16 at 12:21
  • 1
    @Henry Since no answer has been posted yet, you don't need to put an edit. Please reword the question and if any part of it is solved, remove it from the question. Just keep relevant parts and the make the question more clear :) – Reza Aghaei Jun 26 '16 at 17:49
  • Hi @RezaAghaei, as per your advice, I don't know if its allowed or not but I did not only update the content, but update also the question description, although it is still relevant to my previous question. Please see the updated question hope you can help me.. thanks very much :) – Henry Jun 30 '16 at 16:09

0 Answers0