private void generateInvoiceBtn_Click(object sender, EventArgs e) { if (appointment == null || displayedAppointment == null) { return; } var invoicePatient = patientService.GetPatientById(appointment.patient_id); var invoiceService = serviceService.GetServiceById(appointment.service_id); serviceCost = invoiceService.cost; var prescriptions = prescriptionService.GetPatientPrescriptionByPatientId(invoicePatient.patient_id); List <string> drugNames = new List <string>(); foreach (var prescription in prescriptions) { drugNames.Add(drugService.GetDrugById(prescription.drug_id).drug_name); drugCost += drugService.GetDrugById(prescription.drug_id).cost; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(); foreach (var drugName in drugNames) { stringBuilder.AppendLine(drugName); } var invoiceTotalCost = drugCost + serviceCost; InvoiceService invoiceDAOService = new InvoiceService(); invoice = new Invoice.Invoice() { patient_id = invoicePatient.patient_id, invoice_date = appointment.appointment_date, service_id = invoiceService.service_id, drug_names = stringBuilder.ToString(), total_cost = invoiceTotalCost }; var created = invoiceDAOService.CreateInvoice(invoice); MessageBox.Show(created ? "Invoice Successfully Generated!" : "Failed to Generate Invoice"); if (!created) { return; } printPreviewDialog1.StartPosition = FormStartPosition.CenterScreen; printPreviewDialog1.Size = new Size(480, 600); ToolStripButton b = new ToolStripButton(); b.Image = Properties.Resources.PrintIcon; b.DisplayStyle = ToolStripItemDisplayStyle.Image; b.Click += printPreview_PrintClick; ((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0); ((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b); printPreviewDialog1.ShowDialog(); }