public int SavePatient(PateintRecord pateintRecord)
 {
     using (context = new MedicalStore_dbEntities())
     {
         context.PateintRecords.Add(pateintRecord);
         context.SaveChanges();
         int SavedPatientID = pateintRecord.Patient.ID;
         return(SavedPatientID);
     }
 }
 /// <summary>
 /// Getting the ID Based Pateint and Saves the Records against it.
 /// </summary>
 /// <param name="pateintID"></param>
 /// <param name="pateintRecord"></param>
 public void AddRecordToPatient(int pateintID, PateintRecord pateintRecord)
 {
     using (context = new MedicalStore_dbEntities())
     {
         Patient existingPateint = context.Patients.Find(pateintID);
         pateintRecord.Patient = existingPateint;
         context.PateintRecords.Add(pateintRecord);
         context.SaveChanges();
     }
 }
        private void btnSavePatient_Click(object sender, EventArgs e)
        {
            //Check Validation
            Control control = CheckValidity();

            if (control != null)
            {
                control.Focus();
                control.BackColor = Color.LightGray;
                lblStatus.Text    = "Some fields are not filled please view them carefully.";
                return;
            }

            #region Filter the selected medicines and assign to the pateint

            //Getting the Prescribed medicinese from the list
            List <Medicine> PateintSelectedMedicine = new List <Medicine>();

            foreach (var item in lstSelectedMedicines.Items)
            {
                Medicine medicine = new Medicine();
                medicine = medicines.Where(m => m.MedName == item.ToString()).First();

                billAmount = Convert.ToDecimal(billAmount + medicine.MedSellPrice);

                processMedicine.UpdateMedicine(medicine, true);
                //adding to the final list of pateint
                PateintSelectedMedicine.Add(medicine);
            }
            #endregion

            try
            {
                #region Save New Records
                var commandButton = sender as Button;
                if (commandButton.Text == "Save")
                {
                    //Saving the data into the database.
                    PateintRecord pateintRecord = new PateintRecord();
                    pateintRecord.Patient = new Patient();

                    //Getting the data for the Pateint
                    pateintRecord.Patient.PatientName = txtPatName.Text;
                    //Getting the Enum value for the pateint type
                    pateintRecord.Patient.PatientType    = processPatient.getPateintType(cmboBxPatType.Text);
                    pateintRecord.Patient.PatientAddress = txtPatResid.Text;
                    pateintRecord.Patient.FatherName     = txtFatherName.Text;

                    // getting the data for the Pateint record
                    pateintRecord.AppointmentDate = datePickerAppointment.Value;
                    pateintRecord.Description     = txtPatDescription.Text;
                    pateintRecord.Diagnosis       = txtPatDiagnosis.Text;
                    pateintRecord.Treatment       = txtPatTreatment.Text;


                    // storing the selected medicines in the object
                    pateintRecord.RecordsMedicines = new List <RecordsMedicine>();


                    var medicinesIDs = PateintSelectedMedicine.Select(x => x.ID).ToList();
                    //adding medicines to the model object
                    foreach (var item in medicinesIDs)
                    {
                        pateintRecord.RecordsMedicines.Add(new RecordsMedicine()
                        {
                            MedicineID = item
                        });
                    }
                    int SavedPatientID = processPatient.SavePatient(pateintRecord);
                    lblStatus.Text = "Data saved successfullly!";
                    generateBill(SavedPatientID, PateintSelectedMedicine.Select(i => i.ID).ToArray());
                }
                else if (commandButton.Text == "Update")
                {
                    //Updating the data into the database.
                    PateintRecord pateintRecord = new PateintRecord();

                    //Getting the data for the Pateint
                    int pateintID = int.Parse(txtHiddenID.Text);

                    // getting the data for the Pateint record
                    pateintRecord.AppointmentDate = datePickerAppointment.Value;
                    pateintRecord.Description     = txtPatDescription.Text;
                    pateintRecord.Diagnosis       = txtPatDiagnosis.Text;
                    pateintRecord.Treatment       = txtPatTreatment.Text;


                    // storing the selected medicines in the object
                    pateintRecord.RecordsMedicines = new List <RecordsMedicine>();


                    var medicinesIDs = PateintSelectedMedicine.Select(x => x.ID).ToList();
                    //adding medicines to the model object
                    foreach (var item in medicinesIDs)
                    {
                        pateintRecord.RecordsMedicines.Add(new RecordsMedicine()
                        {
                            MedicineID = item
                        });
                    }
                    processPatient.AddRecordToPatient(pateintID, pateintRecord);
                    lblStatus.Text = "Data saved successfullly!";
                    generateBill(pateintID, PateintSelectedMedicine.Select(i => i.ID).ToArray());
                }

                loadGridData();
                #endregion
                clearFields();
                lblStatus.Text = "Record saved !";
            }

            catch (Exception ex)
            {
                lblStatus.Text = "Error: " + ex.Message;
            }
        }