private void ButtonSave_Click(object sender, EventArgs e) { // Check if the land is duplicate TenantDA tenantDA = new TenantDA(); MessageForm messageForm = new MessageForm(); // If tenant.TenantID is null, it is new tenant. So proceed with Save. // Otherwise, proceed with updating tenant. bool result = false; tenant.Fullname = TextBoxFullname.Text; tenant.Address = TextBoxAddress.Text; tenant.MobileNumber = TextBoxMobileNumber.Text; tenant.Father = TextBoxFather.Text; if (tenant.TenantID < 1) { if (!tenantDA.IsDuplicateTenant(tenant)) { try { result = tenantDA.SaveTenant(tenant); } catch (Exception) { messageForm.MessageText = "ओहो! केही आन्तरिक त्रुटीको कारण नयाँ मोहीको विवरण सुरक्षित गर्न सकिएन।"; messageForm.ShowDialog(); } if (result) { messageForm.MessageText = "नयाँ मोहीको विवरण सफलतापूर्वक सुरक्षित गरियो।"; messageForm.ShowDialog(); } // Clear fields for new entry ClearFields(); } else { messageForm.MessageText = "उक्त मोहीको विवरण पहिले नै सुरक्षित गरिसकेको छ।"; messageForm.ShowDialog(); } } else { try { result = tenantDA.UpdateTenant(tenant); } catch (Exception) { messageForm.MessageText = "केही आन्तरिक त्रुटीको कारण मोहीको विवरण सच्याउन सकिएन।"; messageForm.ShowDialog(); } if (result) { messageForm.MessageText = "उक्त मोहीको विवरण सफलतापूर्वक अद्यावधिक गरियो।"; messageForm.ShowDialog(); } } // If edit mode, then self close the form if (tenant.TenantID > 0) { this.Close(); } }
private void ButtonSave_Click(object sender, EventArgs e) { // Either register new tenant or transfer ownership from // existing tenant to new tenant. // If current tenant is empty, we must register new tenant // otherwise we must transfer ownership. LeaseDA leaseDA = new LeaseDA(); Land land = (Land)ComboBoxLand.SelectedItem; Tenant newTenant = (Tenant)ComboBoxNewTenant.SelectedItem; MessageForm messageForm; bool result = false; Lease newLease = new Lease(); newLease.LeaseLand = land; newLease.LeaseTenant = newTenant; newLease.LeaseFrom = TextBoxLeaseFrom.Text; newLease.AnnualRent = TextBoxAnnualRent.Text; newLease.Remarks = TextBoxRemarks.Text; if (ComboBoxCurrentTenant.Items.Count > 0) { // Check the action and act accordingly // Transfer tenant ownership if (RadioButtonActionTransferRegistration.Checked) { // Check whether this is the case of self-transfer to one of the existing tenants bool caseOfSelfTransfer = false; foreach (var item in ComboBoxCurrentTenant.Items) { if (((Tenant)item).Fullname == newTenant.Fullname) { caseOfSelfTransfer = true; break; } } Tenant currentTenant = (Tenant)ComboBoxCurrentTenant.SelectedItem; if (caseOfSelfTransfer) { result = leaseDA.SelfTransfer(currentTenant, newLease); } else { result = leaseDA.TransferLease(currentTenant, newLease); } if (result) { messageForm = new MessageForm(); messageForm.MessageText = "मोही हस्तान्तरणको विवरण सफलतापूर्वक सुरक्षित गरियो।"; messageForm.ShowDialog(); } } // Joint registration if (RadioButtonActionJointRegistration.Checked) { // Check if this lease is already assigned if (!leaseDA.IsDuplicateLease(newLease)) { // Save new lease try { result = leaseDA.SaveLease(newLease); } catch (Exception) { messageForm = new MessageForm(); messageForm.MessageText = "ओहो! केही आन्तरिक त्रुटीको कारण मोही दर्ताको विवरण सुरक्षित गर्न सकिएन।"; messageForm.ShowDialog(); } if (result) { messageForm = new MessageForm(); messageForm.MessageText = "मोही दतार्को विवरण सफलतापूर्वक सुरक्षित गरियो।"; messageForm.ShowDialog(); } // Clear fields for new entry ClearFields(); } else { // duplicate lease messageForm = new MessageForm(); messageForm.MessageText = $"उक्त जग्गा पहिले नै {newTenant.Fullname}को नाममा दर्ता भइसकेको छ।"; messageForm.ShowDialog(); } } } else { // Register new tenant // Check if this lease is already assigned if (!leaseDA.IsDuplicateLease(newLease)) { // Save new lease try { result = leaseDA.SaveLease(newLease); } catch (Exception) { messageForm = new MessageForm(); messageForm.MessageText = "ओहो! केही आन्तरिक त्रुटीको कारण मोही दर्ताको विवरण सुरक्षित गर्न सकिएन।"; messageForm.ShowDialog(); } if (result) { messageForm = new MessageForm(); messageForm.MessageText = "मोही दतार्को विवरण सफलतापूर्वक सुरक्षित गरियो।"; messageForm.ShowDialog(); } // Clear fields for new entry ClearFields(); } else { // duplicate lease messageForm = new MessageForm(); messageForm.MessageText = $"उक्त जग्गा पहिले नै {newTenant.Fullname}को नाममा दर्ता भइसकेको छ।"; messageForm.ShowDialog(); } } }