public bool IsDuplicateRenewal(UnitHolderRegistration regObj, UnitRenewal renwalObj)
        {
            DataTable dtRenewal = commonGatewayObj.Select("SELECT * FROM RENEWAL WHERE REG_BK='" + regObj.FundCode.ToString().ToUpper() + "'AND REG_BR='" + regObj.BranchCode.ToString() + "' AND REN_NO='" + renwalObj.RenewalNo.ToString() + "'");

            if (dtRenewal.Rows.Count > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public void SaveUnitRenewal(UnitHolderRegistration unitRegObj, DataTable dtTransferSaleCert, UnitRenewal renwalObj, DataTable dtDinomination, UnitUser unitUserObj)
        {
            Hashtable      htUnitSaleCert    = new Hashtable();
            Hashtable      htRenwal          = new Hashtable();
            Hashtable      htRenewalIN       = new Hashtable();
            Hashtable      htRenewalOUT      = new Hashtable();
            UnitTransferBL unitTransferBLObj = new UnitTransferBL();
            int            SL_TR_RN          = 0;
            int            CertNo            = 0;
            string         certType          = "";

            string[] saleNoArray;
            string[] certArray;
            string   valid        = "N";
            string   remarks      = "RENEWAL NO:" + renwalObj.RenewalNo.ToString();
            string   Old_SL_TR_RN = "";
            string   FORM_CODE    = "";

            if (renwalObj.RenewalType.ToString().ToUpper() == "S" || renwalObj.RenewalType.ToString().ToUpper() == "C")
            {
                FORM_CODE = "UNIT-UMA";
            }
            else if (renwalObj.RenewalType.ToString().ToUpper() == "S")
            {
                FORM_CODE = "UNIT-CHA";
            }
            try
            {
                commonGatewayObj.BeginTransaction();
                for (int loop = 0; loop < dtTransferSaleCert.Rows.Count; loop++)
                {
                    if (dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().ToUpper().IndexOf('S') >= 0)
                    {
                        htUnitSaleCert = new Hashtable();
                        htUnitSaleCert.Add("VALID", valid);
                        htUnitSaleCert.Add("REMARKS", remarks);


                        saleNoArray = dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().Split('S');
                        SL_TR_RN    = Convert.ToInt32(saleNoArray[1].ToString());
                        certArray   = dtTransferSaleCert.Rows[loop]["CERTIFICATE"].ToString().Split('-');
                        CertNo      = Convert.ToInt32(certArray[1].ToString());
                        certType    = certArray[0].ToString().ToUpper();
                        commonGatewayObj.Update(htUnitSaleCert, "SALE_CERT", "REG_NO=" + Convert.ToInt32(unitRegObj.RegNumber) + " AND REG_BK='" + unitRegObj.FundCode.ToString().ToUpper() + "' AND REG_BR='" + unitRegObj.BranchCode.ToString().ToUpper() + "' AND  SL_NO=" + SL_TR_RN + " AND CERT_NO=" + CertNo + " AND CERT_TYPE='" + certType + "'");

                        Old_SL_TR_RN = "S" + SL_TR_RN.ToString();
                    }
                    else if (dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().ToUpper().IndexOf('T') >= 0)
                    {
                        htUnitSaleCert = new Hashtable();
                        htUnitSaleCert.Add("VALID", valid);
                        htUnitSaleCert.Add("REMARKS", remarks);


                        saleNoArray = dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().Split('T');
                        SL_TR_RN    = Convert.ToInt32(saleNoArray[1].ToString());
                        certArray   = dtTransferSaleCert.Rows[loop]["CERTIFICATE"].ToString().Split('-');
                        CertNo      = Convert.ToInt32(certArray[1].ToString());
                        certType    = certArray[0].ToString().ToUpper();


                        commonGatewayObj.Update(htUnitSaleCert, "TRANS_CERT", "F_CD='" + unitRegObj.FundCode.ToString().ToUpper() + "' AND TR_NO=" + SL_TR_RN + "  AND CERT_NO=" + CertNo + " AND CERT_TYPE='" + certType + "'");
                        //  commonGatewayObj.Update(htUnitSaleCert, "SALE_CERT", " REG_BK='" + regObj.FundCode.ToString().ToUpper() + "' AND REG_BR='" + regObj.BranchCode.ToString().ToUpper() + "' AND SL_NO=" + SL_TR_RN + " AND CERT_NO=" + CertNo + " AND CERT_TYPE='" + certType + "'");
                        //   commonGatewayObj.Update(htUnitSaleCert, "RENEWAL_OUT", "REN_NO='" + SL_TR_RN + "' AND REG_BK='" + regObj.FundCode.ToString().ToUpper() + "' AND REG_BR='" + regObj.BranchCode.ToString().ToUpper() + "' AND CERT_NO=" + CertNo + " AND CERT_TYPE='" + certType + "'");

                        Old_SL_TR_RN = "T" + SL_TR_RN.ToString();
                    }
                    else if (dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().ToUpper().IndexOf('R') >= 0)
                    {
                        htUnitSaleCert = new Hashtable();
                        htUnitSaleCert.Add("VALID", valid);
                        htUnitSaleCert.Add("REMARKS", remarks);


                        saleNoArray = dtTransferSaleCert.Rows[loop]["SL_NO"].ToString().Split('R');
                        SL_TR_RN    = Convert.ToInt32(saleNoArray[1].ToString());
                        certArray   = dtTransferSaleCert.Rows[loop]["CERTIFICATE"].ToString().Split('-');
                        CertNo      = Convert.ToInt32(certArray[1].ToString());
                        certType    = certArray[0].ToString().ToUpper();

                        commonGatewayObj.Update(htUnitSaleCert, "RENEWAL_OUT", "REN_NO='" + SL_TR_RN + "' AND REG_BK='" + unitRegObj.FundCode.ToString().ToUpper() + "' AND REG_BR='" + unitRegObj.BranchCode.ToString().ToUpper() + "' AND CERT_NO=" + CertNo + " AND CERT_TYPE='" + certType + "'");

                        Old_SL_TR_RN = "R" + SL_TR_RN.ToString();
                    }

                    htRenewalIN = new Hashtable();
                    htRenewalIN.Add("REG_BK", unitRegObj.FundCode.ToString().ToUpper());
                    htRenewalIN.Add("REG_BR", unitRegObj.BranchCode.ToString().ToUpper());
                    htRenewalIN.Add("REG_NO", Convert.ToInt32(unitRegObj.RegNumber.ToString()));
                    htRenewalIN.Add("REN_NO", renwalObj.RenewalNo.ToString());

                    certArray = dtTransferSaleCert.Rows[loop]["CERTIFICATE"].ToString().Split('-');

                    htRenewalIN.Add("CERT_TYPE", certArray[0].ToString().ToUpper());
                    htRenewalIN.Add("CERT_NO", Convert.ToInt32(certArray[1].ToString()));
                    htRenewalIN.Add("CERTIFICATE", dtTransferSaleCert.Rows[loop]["CERTIFICATE"].ToString().ToUpper());
                    htRenewalIN.Add("QTY", Convert.ToInt32(dtTransferSaleCert.Rows[loop]["QTY"].ToString()));
                    htRenewalIN.Add("SL_TR_NO", Old_SL_TR_RN.ToString().ToUpper());

                    commonGatewayObj.Insert(htRenewalIN, "RENEWAL_IN");
                }

                DataTable dtTransfer = unitTransferBLObj.dtTrnasfer(dtTransferSaleCert);
                for (int i = 0; i < dtTransfer.Rows.Count; i++) //Insert Into Renewal Table
                {
                    htRenwal = new Hashtable();

                    htRenwal.Add("REN_NO", renwalObj.RenewalNo.ToString());
                    htRenwal.Add("REN_DT", Convert.ToDateTime(renwalObj.RenewalDate).ToString("dd-MMM-yyyy"));
                    htRenwal.Add("REG_BK", unitRegObj.FundCode.ToString().ToUpper());
                    htRenwal.Add("REG_BR", unitRegObj.BranchCode.ToString());
                    htRenwal.Add("REG_NO", Convert.ToInt32(unitRegObj.RegNumber.ToString()));
                    htRenwal.Add("FORM_CODE", FORM_CODE);
                    htRenwal.Add("SL_TR_NO", dtTransfer.Rows[i]["SL_NO"].ToString());
                    htRenwal.Add("REN_TYPE", renwalObj.RenewalType.ToString().ToUpper());
                    htRenwal.Add("QTY", Convert.ToInt32(dtTransfer.Rows[i]["QTY"].ToString()));
                    htRenwal.Add("USER_NM", unitUserObj.UserID.ToString());
                    htRenwal.Add("ENT_DT", DateTime.Now.ToString());
                    htRenwal.Add("ENT_TM", DateTime.Now.ToShortTimeString().ToString());
                    commonGatewayObj.Insert(htRenwal, "RENEWAL");
                }

                for (int looper = 0; looper < dtDinomination.Rows.Count; looper++)//Insert Into RenewalOut Table
                {
                    htRenewalOUT = new Hashtable();
                    htRenewalOUT.Add("REG_BK", unitRegObj.FundCode.ToString().ToUpper());
                    htRenewalOUT.Add("REG_BR", unitRegObj.BranchCode.ToString());
                    htRenewalOUT.Add("REG_NO", Convert.ToInt32(unitRegObj.RegNumber.ToString()));
                    htRenewalOUT.Add("REN_NO", renwalObj.RenewalNo.ToString());
                    htRenewalOUT.Add("CERT_TYPE", dtDinomination.Rows[looper]["dino"].ToString().ToUpper());
                    htRenewalOUT.Add("CERT_NO", Convert.ToInt32(dtDinomination.Rows[looper]["cert_no"].ToString()));
                    htRenewalOUT.Add("CERTIFICATE", SaleCertNo(Convert.ToInt32(dtDinomination.Rows[looper]["cert_no"].ToString()), dtDinomination.Rows[looper]["dino"].ToString().ToUpper()));
                    htRenewalOUT.Add("QTY", Convert.ToInt32(dtDinomination.Rows[looper]["cert_weight"].ToString()));

                    commonGatewayObj.Insert(htRenewalOUT, "RENEWAL_OUT");
                }

                commonGatewayObj.CommitTransaction();
            }
            catch (Exception ex)
            {
                commonGatewayObj.RollbackTransaction();
                throw ex;
            }
        }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        UnitHolderRegistration regObj = new UnitHolderRegistration();

        regObj.FundCode   = fundCodeTextBox.Text;
        regObj.BranchCode = branchCodeTextBox.Text;
        regObj.RegNumber  = regNoTextBox.Text;

        renwalObj                = new UnitRenewal();
        renwalObj.RenewalNo      = renewalNumberTextBox.Text.Trim().ToString();
        renwalObj.RenewalDate    = renewalDateTextBox.Text.Trim().ToString();
        renwalObj.RenewalType    = renewalTypeDropDownList.SelectedValue.ToString().ToUpper();
        renwalObj.RenewalUnitQty = Convert.ToInt32(renewalUnitsTextBox.Text.Trim());

        try
        {
            if (renewalBLObj.IsRenewalLock(regObj))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('" + msgObj.Error().ToString() + " " + "Renewal Operation is Locked " + "');", true);
            }
            else if (renewalBLObj.IsDuplicateRenewal(regObj, renwalObj))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('" + msgObj.Duplicate().ToString() + " " + "Renewal Number " + "');", true);
            }
            else
            {
                int       rowNumber = 1;
                int       line      = 0;
                DataTable dtDino    = opendMFDAO.getTableDinomination();
                DataRow   drDino;
                bool      invalidCert       = false;
                bool      allocateCert      = true;
                int       duplicateCerNoReg = 0;
                string    dino  = "";
                string    cerNo = "";
                foreach (DataGridItem gridItem in dinoGridView.Items)
                {
                    drDino = dtDino.NewRow();
                    TextBox txtDino   = null;
                    TextBox txtCert   = null;
                    TextBox txtWeight = null;
                    txtDino           = (TextBox)(gridItem.FindControl("dinoTextBox"));
                    txtCert           = (TextBox)(gridItem.FindControl("certNoTextBox"));
                    txtWeight         = (TextBox)(gridItem.FindControl("weightTextBox"));
                    duplicateCerNoReg = opendMFDAO.duplicateCerNoReg(regObj, txtDino.Text.ToString(), txtCert.Text.ToString());
                    allocateCert      = unitSaleBLObj.IsCertificateAllocate(regObj, txtDino.Text.ToString(), txtCert.Text.ToString());
                    if (duplicateCerNoReg == 0)
                    {
                        if (opendMFDAO.validationDino(txtDino.Text.ToString().ToUpper(), regObj.FundCode.ToString().ToUpper()) && opendMFDAO.validationWeight(Convert.ToInt32(txtWeight.Text.ToString()), regObj.FundCode.ToString().ToUpper()))
                        {
                            if (allocateCert)
                            {
                                drDino["dino"]        = txtDino.Text.Trim().ToString().ToUpper();
                                drDino["cert_no"]     = Convert.ToInt32(txtCert.Text.Trim().ToString());
                                drDino["cert_weight"] = Convert.ToInt32(txtWeight.Text.Trim().ToString());
                                dtDino.Rows.Add(drDino);
                                rowNumber++;
                            }
                            else
                            {
                                allocateCert = false;
                                line         = rowNumber;
                                dino         = txtDino.Text.Trim().ToString();
                                cerNo        = txtCert.Text.Trim().ToString();
                                break;
                            }
                        }
                        else
                        {
                            invalidCert = true;
                            line        = rowNumber;
                            break;
                        }
                    }
                    else
                    {
                        dino  = txtDino.Text.Trim().ToString();
                        cerNo = txtCert.Text.Trim().ToString();
                        break;
                    }
                }

                if (duplicateCerNoReg > 0)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('Certificate " + dino + "-" + cerNo + " is Already Used to Reg No:" + duplicateCerNoReg + "');", true);
                }
                else
                {
                    if (invalidCert)
                    {
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('Save Failed!!Invalid Dinomination or Weight at Line: " + line + "');", true);
                    }
                    else if (!allocateCert)
                    {
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('Save Failed!! Line: " + line + " and Certificate No: " + dino + "-" + cerNo + " is not allocated in this branch');", true);
                    }
                    else if (!invalidCert && allocateCert)
                    {
                        DataTable dtGrid = opendMFDAO.getTableDataGrid();
                        DataRow   drGrid;
                        foreach (DataGridItem gridRow in leftDataGrid.Items)
                        {
                            CheckBox leftCheckBox = (CheckBox)gridRow.FindControl("leftCheckBox");
                            if (leftCheckBox.Checked)
                            {
                                drGrid                = dtGrid.NewRow();
                                drGrid["SL_NO"]       = gridRow.Cells[1].Text.Trim().ToString();
                                drGrid["CERTIFICATE"] = gridRow.Cells[2].Text.Trim().ToString();
                                drGrid["QTY"]         = gridRow.Cells[3].Text.Trim().ToString();
                                dtGrid.Rows.Add(drGrid);
                            }
                        }

                        renewalBLObj.SaveUnitRenewal(regObj, dtGrid, renwalObj, dtDino, userObj);
                        // unitSaleBLObj.SaveUnitSale(regObj, saleObj, dtDino, userObj);
                        ClearText();
                        leftDataGrid.Visible      = false;
                        renewalNumberTextBox.Text = "";
                        regNoTextBox.Text         = "";
                        //saleNumberTextBox.Text = unitSaleBLObj.getNextSaleNo(regObj, userObj).ToString();
                        //saleObj.SaleNo = unitSaleBLObj.getNextSaleNo(regObj, userObj) - 1;
                        //saleDateTextBox.Text = opendMFDAO.getLastSaleDate(regObj, saleObj).ToString("dd-MMM-yyyy");
                        //saleRateTextBox.Text = opendMFDAO.getLastSaleRate(regObj, saleObj).ToString();
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert('" + msgObj.Success().ToString() + "');", true);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Popup", "alert ('" + msgObj.Error().ToString() + " " + ex.Message.Replace("'", "").ToString() + "');", true);
        }
    }