private void button13_Click(object sender, EventArgs e)
 {
     if (ledgerId > 0)
     {
         SupplierDetails ss = new SupplierDetails();
         ss.dataGridBind();
     }
     this.Close();
 }
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtLedgerName.Text == string.Empty && txtNotes.Text == string.Empty && txtOpeningBal.Text == string.Empty)
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(txtLedgerName, "Enter all details.");
                    txtLedgerName.Focus();
                    panel3.Visible = true;
                    lblError.Text  = "Enter all details.";
                }
                else if (txtLedgerName.Text == string.Empty)
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(txtLedgerName, "Enter ledger-name");
                    txtLedgerName.Focus();
                    panel3.Visible = true;
                    lblError.Text  = "Enter ledger-name.";
                }
                else if (txtOpeningBal.Text == string.Empty)
                {
                    errorProvider1.Clear();
                    errorProvider1.SetError(txtOpeningBal, "Enter opening balance.");
                    txtOpeningBal.Focus();
                    panel3.Visible = true;
                    lblError.Text  = "Enter opening balance.";
                }
                else
                {
                    if (btnCreate.Text == "Create")
                    {
                        _entities = new MicroAccountsEntities1();

                        var gId       = Convert.ToInt32(cmbParentGroup.SelectedValue);
                        var checkData = _entities.tbl_AccLedger.Where(x => x.ledgerName == txtLedgerName.Text.Trim().ToString() && x.groupId == gId).FirstOrDefault();

                        if (checkData != null)
                        {
                            MessageBox.Show("Ledger already Exists. Cannot create ledger with this name");
                            return;
                        }


                        tbl_AccLedger accLedgerData = new tbl_AccLedger();
                        accLedgerData.ledgerName = txtLedgerName.Text.Trim().ToString();
                        accLedgerData.groupId    = Convert.ToInt32(cmbParentGroup.SelectedValue);
                        accLedgerData.opBalance  = Convert.ToDecimal(txtOpeningBal.Text);

                        if (cmbDRCR.SelectedItem.ToString() == "Dr")
                        {
                            accLedgerData.opBalanceDC = "D";
                        }
                        else
                        {
                            accLedgerData.opBalanceDC = "C";
                        }

                        accLedgerData.notes = txtNotes.Text.Trim().ToString();

                        if (chkBankOrCash.Checked)
                        {
                            accLedgerData.type = 1;
                        }
                        else
                        {
                            accLedgerData.type = 0;
                        }
                        accLedgerData.createdDate = DateTime.Now;
                        _entities.tbl_AccLedger.Add(accLedgerData);
                        _entities.SaveChanges();

                        tbl_LedgerDetails ledgerDetails = new tbl_LedgerDetails();

                        if (txtAddress.Text.Trim() == string.Empty)
                        {
                            ledgerDetails.address = "--";
                        }
                        else
                        {
                            ledgerDetails.address = txtAddress.Text.Trim().ToString();
                        }
                        if (txtContact.Text == string.Empty)
                        {
                            ledgerDetails.contact = 0;
                        }
                        else
                        {
                            ledgerDetails.contact = Convert.ToDecimal(txtOpeningBal.Text);
                        }

                        ledgerDetails.ledgerId    = _entities.tbl_AccLedger.Where(x => x.ledgerName == txtLedgerName.Text && x.groupId == accLedgerData.groupId).FirstOrDefault().Id;
                        ledgerDetails.createdDate = DateTime.Now;
                        ledgerDetails.updateDate  = DateTime.Now;

                        _entities.tbl_LedgerDetails.Add(ledgerDetails);
                        _entities.SaveChanges();

                        MessageBox.Show("Record Added Successfully");
                    }
                    else
                    {
                        //Update Code

                        _entities = new MicroAccountsEntities1();

                        var data = _entities.tbl_AccLedger.Where(x => x.Id == ledgerId).FirstOrDefault();

                        data.ledgerName = txtLedgerName.Text.Trim().ToString();
                        data.groupId    = Convert.ToInt32(cmbParentGroup.SelectedValue);
                        data.opBalance  = Convert.ToDecimal(txtOpeningBal.Text);

                        if (cmbDRCR.SelectedItem.ToString() == "Dr")
                        {
                            data.opBalanceDC = "D";
                        }
                        else
                        {
                            data.opBalanceDC = "C";
                        }

                        data.notes       = txtNotes.Text.Trim().ToString();
                        data.updatedDate = DateTime.Now;

                        _entities.SaveChanges();

                        var dataLedgerDetails = _entities.tbl_LedgerDetails.Where(x => x.ledgerId == ledgerId).FirstOrDefault();

                        dataLedgerDetails.address    = txtAddress.Text.Trim().ToString();
                        dataLedgerDetails.contact    = Convert.ToDecimal(txtContact.Text.Trim());
                        dataLedgerDetails.ledgerId   = _entities.tbl_AccLedger.Where(x => x.ledgerName == txtLedgerName.Text && x.groupId == data.groupId).FirstOrDefault().Id;
                        dataLedgerDetails.updateDate = DateTime.Now;

                        _entities.SaveChanges();

                        MessageBox.Show("Record Updated Successfully");
                        SupplierDetails ss = new SupplierDetails();
                        ss.dataGridBind();
                    }
                    bindComboBox();
                    clearTextBox();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something went wrong. Contact your system administrator");
            }
        }