4
Reply

Checking if record exist (entity framework)

Peter Dzuya

Peter Dzuya

Jul 20 2015 5:49 AM
4.5k
Hi Experts I am new to C#. I have a Form that add new a customer . But I want to check if that customer already exist. How do I do it from the the code shown here below. Thanks
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars.Alerter;
using DevExpress.XtraEditors;
using MakGarm.Business;
using MakGarm.Business.Contracts;
using MakGarm.Common.Entities.Core;
using MakGarm.Common.Entities.GeneralLedgerAccounts;
using MakGarm.Common.Entities.Miscellaneous;
using MakGarm.Common.Entities.SubsidiaryLedgerAccounts;
using Microsoft.Practices.Unity;
using Uwapi.Utils;
 
namespace MakGarm.Start.Customers {
 
public partial class frmAddCustomer : DevExpress.XtraEditors.XtraForm {
 
private readonly ICustomerService _customerService;
 
private readonly Customer _customer;
private readonly IContactService _contactService;
 
private readonly IFinancialMonthService _financialMonthService;
private readonly FinancialYear _financialYear;
 
public int NewcontactId;
public int CheckCustomerId;
 
public frmAddCustomer() {
InitializeComponent();
 
_customerService = UnityConfig.GetContainer().Resolve<ICustomerService>();
_contactService = UnityConfig.GetContainer().Resolve<IContactService>();
_financialMonthService = UnityConfig.GetContainer().Resolve<IFinancialMonthService>();
 
_customer = new Customer() {
Active = true,
Accountclosed = false,
Datecreated = DateTime.Now,
CreditDays = 0,
CreditLimit = 0,
LastEditedDate = DateTime.Now,
CompanyBranchId = CurrentUserInfo.User.CompanyBranch.CompanyId,
CheckerUserId = CurrentUserInfo.User.UserId,
LastEditorUserId = CurrentUserInfo.User.UserId,
};
}
 
private void frmAddCustomer_Load(object sender, EventArgs e){
 
customerBindingSource.DataSource = _customer;
ContactBindingSource.DataSource = _contactService.GetAll();
FinacialMonthsbindingSource.DataSource = _financialMonthService.GetCurrentFinancialMonth();
}
 
private void simpleButtonAcccept_Click(object sender, EventArgs e){
 
_customer.ContactId = NewcontactId;
_customer.BillingStartFinancialMonthId = _financialMonthService.GetCurrentFinancialMonth().FinancialMonthId;
 
customerBindingSource.EndEdit();
_customerService.Insert(_customer);
 
 
AlertButton Btn = new AlertButton();
Btn.Hint = "Open File";
Btn.Name = "Button Open";
alertControl1.Buttons.Add(Btn);
alertControl1.ButtonClick += new AlertButtonClickEventHandler(alertControl1_ButtonClick);
AlertInfo info = new AlertInfo("GMIS 2015 Alert",
"The new job card record has been Added successfully");
alertControl1.Show(this, info);
 
this.Close();
 
}
 
 
private void alertControl1_ButtonClick(object sender, AlertButtonClickEventArgs e) {
throw new NotImplementedException();
}
private void contactNameSearchLookUpEdit_EditValueChanged(object sender, EventArgs e){
var selectedContact = contactNameSearchLookUpEditView.GetFocusedRow() as Contact;
if (selectedContact == null) return;
NewcontactId = selectedContact.ContactId;
}
 
 
private void simpleButtonCancel_Click(object sender, EventArgs e) {
if(MessageBox.Show("Do you really want to cancel this process","GMIS - Cancel",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes) {
contactNameSearchLookUpEdit.Text = string.Empty;
remarksMemoEdit.Text = string.Empty;
}
}
 
 
private void simpleButtonClose_Click(object sender, EventArgs e) {
 
if (MessageBox.Show("Are You sure", "GMIS - Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes) {
this.Close();
}
 
}
}
}
 

Answers (4)