private void PreviewAgreementButton_Click(object sender, EventArgs e) { try { if (TenantGridView.CurrentRow == null || PropertyGridView.CurrentRow == null) { return; } var tenant = (TenantModel)TenantGridView.CurrentRow.DataBoundItem; var property = (PropertyModel)PropertyGridView.CurrentRow.DataBoundItem; var pdfService = new PdfService(); var pdfDoc = pdfService.GenerateRentalDocument(property.OwnerName, tenant.Name); var pdfFileName = $"rental_agreement_{property.OwnerName}_{tenant.Name}.pdf"; System.IO.File.WriteAllBytes(pdfFileName, pdfDoc); var pdfPreviewForm = new PdfDocPreviewForm(pdfFileName); pdfPreviewForm.ShowDialog(this); if (System.IO.File.Exists(pdfFileName)) { System.IO.File.Delete(pdfFileName); } } catch (Exception ex) { //log ex MessageBox.Show("System error has occurred", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }