using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Imaging; using System.Net.NetworkInformation; namespace Louis__Pharmacy_CNSA212_FP { public partial class frmPrescription : Form { private readonly bool isAdd; private frmInfo SourceForm; ErrorProvider epLocal = new ErrorProvider(); public frmPrescription(frmInfo sourceForm, bool isNew) { SourceForm = sourceForm; isAdd = isNew; InitializeComponent(); if (isNew) { lblDisPurpose.Text = "Add Prescription"; btnGO.Text = "Create"; } else { lblDisPurpose.Text = "Edit Prescription"; btnGO.Text = "Update"; } KeyPreview = true; KeyDown += frmPrescription_KeyDown; txtRxNum.Enabled = false; txtCompletedRefills.Text = "0"; } private void frmPrescription_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) Close(); if (e.KeyCode == Keys.Enter) btnGO_Click(sender, e); } public void FillPrescription(string rxNum, string medID, string physID, string patID) { var ds = new DataSet(); var data = new PharmacyDataTier(); ds = PharmacyDataTier.PrescriptionInfoSearch(rxNum,medID,physID,patID); txtRxNum.Text = ds.Tables[0].Rows[0]["RxNum"].ToString(); txtPatID.Text = ds.Tables[0].Rows[0]["Patient_id"].ToString(); txtMedID.Text = ds.Tables[0].Rows[0]["Medication_id"].ToString(); txtPhysName.Text = ds.Tables[0].Rows[0]["Physician_name"].ToString(); txtPhysID.Text = ds.Tables[0].Rows[0]["Physician_id"].ToString(); txtCompletedRefills.Text = ds.Tables[0].Rows[0]["Completed_refills"].ToString(); txtMaxRefills.Text = ds.Tables[0].Rows[0]["Max_refills"].ToString(); } public void FillPrescription(string rxNum) { var ds = new DataSet(); var data = new PharmacyDataTier(); ds = PharmacyDataTier.PrescriptionInfoSearch(rxNum); txtRxNum.Text = ds.Tables[0].Rows[0]["RxNum_id"].ToString(); txtPatID.Text = ds.Tables[0].Rows[0]["Patient_id"].ToString(); txtMedID.Text = ds.Tables[0].Rows[0]["Medication_id"].ToString(); txtPhysName.Text = ds.Tables[0].Rows[0]["PrescribedBy"].ToString(); txtPhysID.Text = ds.Tables[0].Rows[0]["Physician_id"].ToString(); txtCompletedRefills.Text = ds.Tables[0].Rows[0]["pastNumRefills"].ToString(); txtMaxRefills.Text = ds.Tables[0].Rows[0]["numRefills"].ToString(); } private void btnCancel_Click(object sender, EventArgs e) { Close(); } // private void btnGO_Click(object sender, EventArgs e) // { // // var hasFailed = false; // // var Patient_id = ""; // var Medication_id = ""; // var Physician_name = ""; // var Physician_id = ""; // var Completed_refills = ""; // var Max_refills = ""; // var RxNum = ""; // // // if (txtMedID.Text.Length + txtPhysName.Text.Length + txtMedID.Text.Length + txtPhysID.Text.Length + txtCompletedRefills.Text.Length + txtMaxRefills.Text.Length + txtRxNum.Text.Length > 0) // { // // try // { // // Patient_id = txtPatID.Text; // // if (Patient_id.Length > 8) // { // throw new Exception(); // }else // while (Patient_id.Length < 8) // { // Patient_id = "0" + Patient_id; // } // // } // catch (Exception exception) // { // epLocal.SetError(txtPatID, "Invalid Value"); // hasFailed = true; // } // // try // { // RxNum = txtRxNum.Text; // // if (RxNum.Length > 11) // { // throw new Exception(); // } // else // { // while (RxNum.Length < 11) // { // RxNum = "0" + RxNum; // } // } // // } // catch (Exception exception) // { // epLocal.SetError(txtRxNum, "Invalid Value"); // hasFailed = true; // } // try // { // // Physician_id = txtPhysID.Text; // // if (Physician_id.Length > 8) // { // throw new Exception(); // }else // while (Physician_id.Length < 8) // { // Physician_id = "0" + Physician_id; // } // // } // catch (Exception exception) // { // epLocal.SetError(txtPhysID, "Invalid Value"); // hasFailed = true; // } // try // { // Physician_name = txtPhysName.Text; // if (Physician_name.Length > 30) // { // throw new Exception(); // } // // } // catch (Exception exception) // { // epLocal.SetError(txtPhysName, "Invalid Value"); // hasFailed = true; // } // // try // { // Medication_id = txtMedID.Text; // if (Medication_id.Length > 7) // { // throw new Exception(); // } // // while (Medication_id.Length < 7) // { // Medication_id = "0" + Medication_id; // } // } // catch (Exception exception) // { // epLocal.SetError(txtMedID, "Invalid Value"); // hasFailed = true; // } // if (!hasFailed) // { // if (isAdd) // { // PharmacyDataTier.CreatePrescription( // RxNum, // Patient_id, // Medication_id, // Physician_name, // Physician_id, // Completed_refills, // Max_refills); // } // else // { // PharmacyDataTier.UpdatePrescription( // RxNum, // Patient_id, // Medication_id, // Physician_name, // Physician_id, // Completed_refills, // Max_refills); // } // // SourceForm.txtRxPrescriptionID.Text = RxNum; // SourceForm.btnPrescriptionSearch_Click(sender, e); // Close(); // // // } // } // // } private void frmPrescription_Load(object sender, EventArgs e) { //txtMedID.Enabled = false; if (isAdd) { var nextID = PharmacyDataTier.GetNextRxNum(); txtRxNum.Text = nextID.ToString(); } } private bool Exists(string Table, string find) { bool value = false; if (Table == "PHYSICIAN") { DataSet ds = PharmacyDataTier.PhysicianInfoSearch(find); if (ds != null && ds.Tables[0].Rows.Count > 0) { value = true; } }else if (Table == "PATIENT") { DataSet ds = PharmacyDataTier.PatientInfoSearch(find); if (ds != null && ds.Tables[0].Rows.Count > 0) { value = true; } }else if (Table == "MEDICATION") { DataSet ds = PharmacyDataTier.MedicationInfoSearch(find); if (ds != null && ds.Tables[0].Rows.Count > 0) { value = true; } } else { throw new Exception("TableNotFound"); } return value; } private void btnGO_Click(object sender, EventArgs e) { string RxNum_id = ""; int numRefills = 0; int pastNumRefills = 0; string PrescribedBy = ""; string PhysicianID = ""; string Medication_id = ""; string Patient_id = ""; bool hasFailed = false; epLocal.Clear(); if (txtRxNum.Text.Length > 0 && txtRxNum.Text.Length <= 11) { RxNum_id = txtRxNum.Text; while (RxNum_id.Length < 11) { RxNum_id = "0" + RxNum_id; } } if (txtPhysName.Text.Length > 0) { PrescribedBy = txtPhysName.Text; } else { epLocal.SetError(txtPhysName, "Must Enter a Value"); } if (txtPatID.Text.Length > 0) { Patient_id = txtPatID.Text; if (!Exists("PATIENT", Patient_id)) { hasFailed = true; epLocal.SetError(txtPatID, "Patient Does Not Exist"); } else { while (Patient_id.Length < 8) { Patient_id = "0" + Patient_id; } } } else { hasFailed = true; epLocal.SetError(txtPatID, "Must Enter A Patient ID"); } if (txtMedID.Text.Length > 0) { Medication_id = txtMedID.Text; if (!Exists("MEDICATION", Medication_id)) { hasFailed = true; epLocal.SetError(txtMedID, "Medication Does Not Exist"); } else { while (Medication_id.Length < 7) { Medication_id = "0" + Medication_id; } } } else { hasFailed = true; epLocal.SetError(txtMedID, "Must Enter a Medication ID"); } if (txtPhysID.Text.Length > 0) { PhysicianID = txtPhysID.Text; if (!Exists("PHYSICIAN", PhysicianID)) { hasFailed = true; epLocal.SetError(txtPhysID, "Physician Does Not Exist"); } else { while (PhysicianID.Length < 8) { PhysicianID = "0" + PhysicianID; } } } else { hasFailed = true; epLocal.SetError(txtPhysID, "Must Enter a Physician ID"); } try { if (txtCompletedRefills.Text.Length > 0) { pastNumRefills = int.Parse(txtCompletedRefills.Text); } else throw new Exception(); } catch (Exception exception) { epLocal.SetError(txtCompletedRefills, "Invalid Value"); hasFailed = true; } try { if (txtMaxRefills.Text.Length > 0) { numRefills = int.Parse(txtMaxRefills.Text); } if (numRefills < 0) { throw new Exception(); } } catch (Exception exception) { epLocal.SetError(txtMaxRefills, "Invalid Value"); hasFailed = true; } if (!hasFailed) { if (isAdd) { PharmacyDataTier.CreatePrescription( RxNum_id, Patient_id, Medication_id, PrescribedBy, PhysicianID, pastNumRefills, numRefills); } else { PharmacyDataTier.UpdatePrescription( RxNum_id, Patient_id, Medication_id, PrescribedBy, PhysicianID, pastNumRefills, numRefills); } SourceForm.txtRxPrescriptionID.Text = RxNum_id; SourceForm.btnPrescriptionSearch_Click(sender, e); Close(); } } private void txtMedID_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtMedID, ""); } private void txtPhysName_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtPhysName, ""); } private void txtPhysID_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtPhysID, ""); } private void txtCompletedRefills_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtCompletedRefills, ""); } private void txtMaxRefills_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtMaxRefills, ""); } private void txtRxNum_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtRxNum, ""); } private void btnGO_Click_1(object sender, EventArgs e) { } private void txtRxNum_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtRxNum,""); } private void txtPatID_TextChanged(object sender, EventArgs e) { epLocal.SetError(txtPatID,""); } private void txtMedID_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtMedID,""); } private void txtPhysName_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtPhysName,""); } private void txtPhysID_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtPhysID,""); } private void txtCompletedRefills_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtCompletedRefills,""); } private void txtMaxRefills_TextChanged_1(object sender, EventArgs e) { epLocal.SetError(txtMaxRefills,""); } private void btnCancel_Click_1(object sender, EventArgs e) { Close(); } } }