private void GenerateCode()
        {
            Digital_AppEntities posContext = new Digital_AppEntities();
            var suppliers = posContext.FarmerInfoes;

            if (suppliers.Count() == 0)
            {
                txtcode.Text = "FR-" + "1".PadLeft(6, '0');
            }
            else
            {
                FarmerInfo supplier = suppliers.OrderByDescending(id => id.ID).Take(1).Single();
                txtcode.Text = "FR-" + ((Convert.ToInt16(supplier.Code.Substring(4)) + 1).ToString().PadLeft(6, '0'));
            }
        }
        private void EnableButton()
        {
            if (cmbSupplier.SelectedIndex != -1)
            {
                using (var posContext = new Digital_AppEntities())
                {
                    FarmerInfo aFarmerInfo = posContext.FarmerInfoes.SingleOrDefault(s => s.ID == (int)cmbSupplier.SelectedValue);
                    this.btnAdd.Enabled = true;
                    txtMobileNo.Text    = aFarmerInfo.MobileNo;

                    if (dgvItemPurchaseDetail.Rows.Count > 0)
                    {
                        this.btnSave.Enabled   = true;
                        this.btnCancel.Enabled = true;
                    }
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (var posContext = new Digital_AppEntities())
                {
                    FarmerInfo aFarmerInfo;
                    if (supplierID == 0)
                    {
                        int Count = posContext.FarmerInfoes.Count(a => a.MobileNo == txtMobileNo.Text);
                        if (Count > 0)
                        {
                            MessageBox.Show("This MobileNo Already Exist..!!", Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        aFarmerInfo            = new FarmerInfo();
                        aFarmerInfo.Code       = txtcode.Text;
                        aFarmerInfo.FarmerName = txtName.Text;
                        aFarmerInfo.FatherName = txtfathername.Text;
                        aFarmerInfo.MotherName = txtMotherName.Text;
                        aFarmerInfo.MobileNo   = txtMobileNo.Text;
                        aFarmerInfo.Email      = txtEmail.Text;
                        aFarmerInfo.Address    = txtAddress.Text;
                        aFarmerInfo.PostalCode = txtPostalCode.Text;
                        if (cmbCountry.SelectedValue != null)
                        {
                            aFarmerInfo.CountryID = (int)cmbCountry.SelectedValue;
                        }
                        if (cmbDistrict.SelectedValue != null)
                        {
                            aFarmerInfo.DistricID = (int)cmbDistrict.SelectedValue;
                        }
                        if (cmbThana.SelectedValue != null)
                        {
                            aFarmerInfo.ThanaID = (int)cmbThana.SelectedValue;
                        }
                        aFarmerInfo.Active = chkActive.Checked;
                        posContext.FarmerInfoes.Add(aFarmerInfo);
                        MessageBox.Show("Save Successfully", Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        int Count = posContext.FarmerInfoes.Count(a => a.MobileNo == txtMobileNo.Text && a.ID != supplierID);
                        if (Count > 0)
                        {
                            MessageBox.Show("This MobileNo Already Exist..!!", Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        aFarmerInfo            = posContext.FarmerInfoes.Single(id => id.ID == supplierID);
                        aFarmerInfo.Code       = txtcode.Text;
                        aFarmerInfo.FarmerName = txtName.Text;
                        aFarmerInfo.FatherName = txtfathername.Text;
                        aFarmerInfo.MotherName = txtMotherName.Text;
                        aFarmerInfo.MobileNo   = txtMobileNo.Text;
                        aFarmerInfo.Email      = txtEmail.Text;
                        aFarmerInfo.Address    = txtAddress.Text;
                        aFarmerInfo.PostalCode = txtPostalCode.Text;
                        if (cmbCountry.SelectedValue != null)
                        {
                            aFarmerInfo.CountryID = (int)cmbCountry.SelectedValue;
                        }
                        if (cmbDistrict.SelectedValue != null)
                        {
                            aFarmerInfo.DistricID = (int)cmbDistrict.SelectedValue;
                        }
                        if (cmbThana.SelectedValue != null)
                        {
                            aFarmerInfo.ThanaID = (int)cmbThana.SelectedValue;
                        }

                        MessageBox.Show("Update Successfully", Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    aFarmerInfo.Active = chkActive.Checked;
                    posContext.SaveChanges();
                    //this.LoadSuppliers(posContext);
                    isRecordSaved = true;



                    this.ClearControls();
                    this.GenerateCode();
                    txtName.Focus();
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.GetType().Name == "SqlException")
                    {
                        int errorNumber = ((System.Data.SqlClient.SqlException)(ex.InnerException)).Number;
                        switch (errorNumber)
                        {
                        case 2601:
                            MessageBox.Show(MessageManager.GetDuplicateErrorMsg(this.Text.ToLower()), Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;

                        default:
                            MessageBox.Show(MessageManager.CommonExceptionMsg, Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                        }
                    }
                    else
                    {
                        MessageBox.Show(ex.InnerException.Message, Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show(MessageManager.CommonExceptionMsg, Global.ApplicationNameWithVersion, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }