private async Task InsertPatient()
        {
            patients = await patientTable
            .Where(p => p.Patient_num == (patient_num.Text ?? "000"))
            .ToCollectionAsync();

            if (patients.Count > 0)
            {
                msg.Text = "Patient record already exisit.";
                return;
            }
            //New Patient Record

            Patient patient = new Patient();

            patient.Id = Guid.NewGuid().ToString();

            patient.Patient_num = patient_num.Text;
            patient.Encounter_id = encounter_id.Text;
            patient.First_name = first_name.Text;
            patient.Last_name = last_name.Text;
            patient.Address = address.Text;


            patient.Admission_source_id = admission_source_id.Text;


            patient.Admission_type_id = admission_type_id.SelectedIndex.ToString();
            //patient.Admission_type_id = admission_type_id.SelectedValue.ToString();
            patient.DiabetesMed = diabetesMed.Text;
            patient.Diag_1 = diag_1.Text;
            patient.Discharge_disposition_id = discharge_disposition_id.Text;
            patient.Gender = gender.Text;
            patient.Insulin = insulin.Text;
            patient.Metformin = metformin.Text;
            patient.Num_age = num_age.Text;
            patient.Discharge_time = discharge_time.Text;
            patient.Date_of_birth = date_of_birth.Text;

            patient.Num_lab_procedures = num_lab_procedures.Text;
            patient.Num_procedures = num_procedures.Text;
            patient.Number_diagnoses = number_diagnoses.Text;
            patient.Number_emergency = number_emergency.Text;
            patient.Number_inpatient = number_inpatient.Text;
            patient.Number_outpatient = number_outpatient.Text;
            patient.Pioglitazone = pioglitazone.Text;
            patient.Rosiglitazone = rosiglitazone.Text;
            patient.Time_in_hospital = time_in_hospital.Text;
            patient.Bmi = bmi.Text;
            patient.Weight  = weight.Text;
            patient.Height  = height.Text;

            patient.Zipcode = zipcode.Text;
            patient.Pct_calories_from_carbs = pct_calories_from_carbs.Text;
            patient.Daily_minutes_walking = daily_Minutes_walking.Text;
            patient.Sd_glucose = sd_glucose.Text;
            patient.Readmitted = readmitted.Text;
            patient.Imageuri = imageuri.Text;

            // This code inserts a new PatientRecord into the database. When the operation completes
            // and Mobile App backend has assigned an Id, the item is added to the CollectionView.
            await patientTable.InsertAsync(patient);
            patients.Add(patient);

            msg.Text = "Patient record added.";
            //await SyncAsync(); // offline sync
        }
        private async Task UpdatePatient()
        {
            //update patient record

            Patient patient = new Patient();

            patient.Id = id.Text;

            patient.Patient_num = patient_num.Text;
            patient.Encounter_id = encounter_id.Text;
            patient.First_name = first_name.Text;
            patient.Last_name = last_name.Text;
            patient.Address = address.Text;

            patient.Admission_source_id = admission_source_id.Text;
            patient.Admission_type_id = admission_type_id.SelectedIndex.ToString();
            patient.DiabetesMed = diabetesMed.Text;
            patient.Diag_1 = diag_1.Text;
            patient.Discharge_disposition_id = discharge_disposition_id.Text;
            patient.Gender = gender.Text;
            patient.Insulin = insulin.Text;
            patient.Metformin = metformin.Text;
            patient.Num_age = num_age.Text;

            patient.Discharge_time = discharge_time.Text;
            patient.Date_of_birth = date_of_birth.Text;




            patient.Num_lab_procedures = num_lab_procedures.Text;
            patient.Num_procedures = num_procedures.Text;
            patient.Number_diagnoses = number_diagnoses.Text;
            patient.Number_emergency = number_emergency.Text;
            patient.Number_inpatient = number_inpatient.Text;
            patient.Number_outpatient = number_outpatient.Text;
            patient.Pioglitazone = pioglitazone.Text;
            patient.Rosiglitazone = rosiglitazone.Text;
            patient.Time_in_hospital = time_in_hospital.Text;
            patient.Bmi = bmi.Text;

            patient.Weight = weight.Text;
            patient.Height = height.Text;

            patient.Zipcode = zipcode.Text;
            patient.Pct_calories_from_carbs = pct_calories_from_carbs.Text;
            patient.Daily_minutes_walking = daily_Minutes_walking.Text;
            patient.Sd_glucose = sd_glucose.Text;
            patient.Readmitted = readmitted.Text;
            patient.Imageuri = imageuri.Text;

            
            MobileServiceInvalidOperationException exception = null;
            try
            {
                await patientTable.UpdateAsync(patient);
            }
            catch (MobileServiceInvalidOperationException e)
            {
                exception = e;
            }
            //ListItems.Focus(Windows.UI.Xaml.FocusState.Unfocused);
        }