protected void BtncallUpd_Click(object sender, EventArgs e)
        {
            try
            {
                if (BtncallUpd.Text == "UPDATE")
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowModel1", "ShowModel1()", true);
                }
                else
                {
                    #region INSERT_UPDATE PRICE MASTER

                    STOCK_PRICE_MASLogicLayer insert = new STOCK_PRICE_MASLogicLayer();
                    insert.COMP_CODE    = Session["COMP_CODE"].ToString();
                    insert.SRNO         = HfSrNo.Value;
                    insert.FRDT         = Convert.ToDateTime(TxtFromDate.Text.Trim()).ToString("MM-dd-yyyy");
                    insert.TODT         = Convert.ToDateTime(TxtToDate.Text.Trim()).ToString("MM-dd-yyyy");
                    insert.REMARK       = TxtRemark.Text.Trim().ToUpper();
                    insert.ACTIVE       = DdlActive.SelectedValue.Trim().ToUpper();
                    insert.INS_USERID   = Session["USERNAME"].ToString();
                    insert.INS_TERMINAL = Session["PC"].ToString();
                    insert.INS_DATE     = "";
                    insert.UPD_USERID   = Session["USERNAME"].ToString();
                    insert.UPD_TERMINAL = Session["PC"].ToString();
                    insert.UPD_DATE     = "";

                    #endregion
                    #region INSERT_UPDATE PRICE DETAILS

                    XmlDocument    XDoc1 = new XmlDocument();
                    XmlDeclaration dec1  = XDoc1.CreateXmlDeclaration("1.0", null, null);
                    XDoc1.AppendChild(dec1);// Create the root element
                    XmlElement root1 = XDoc1.CreateElement("root");
                    XDoc1.AppendChild(root1);

                    foreach (GridViewRow row in GvStockPriceDet.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {
                            HiddenField HfCompCodeVal = row.FindControl("HfCompCode") as HiddenField;
                            // HiddenField HfSrNoVal = row.FindControl("HfSrNo") as HiddenField;
                            HiddenField HfStockCodeVal = row.FindControl("HfStockCode") as HiddenField;

                            Label lblCategoryName = row.FindControl("lblCategoryName") as Label;
                            Label lblProdCode     = row.FindControl("lblProdCode") as Label;
                            Label lblPartNo       = row.FindControl("lblPartNo") as Label;
                            Label lblProductName  = row.FindControl("lblProductName") as Label;

                            TextBox TxtRate        = row.FindControl("TxtRate") as TextBox;
                            TextBox TxtCustDis     = row.FindControl("TxtCustDis") as TextBox;
                            TextBox TxtCustDisRate = row.FindControl("TxtCustDisRate") as TextBox;
                            TextBox TxtDealerDis   = row.FindControl("TxtDealerDis") as TextBox;
                            TextBox TxtDealerRate  = row.FindControl("TxtDealerRate") as TextBox;
                            TextBox TxtMaxDis      = row.FindControl("TxtMaxDis") as TextBox;
                            TextBox TxtMaxRate     = row.FindControl("TxtMaxRate") as TextBox;

                            XmlElement HandleDetail2 = XDoc1.CreateElement("Detail");
                            HandleDetail2.SetAttribute("COMP_CODE", Session["COMP_CODE"].ToString());


                            // HandleDetail2.SetAttribute("SRNO", (HfSrNoVal.Value));
                            HandleDetail2.SetAttribute("SCODE", (HfStockCodeVal.Value));
                            if (TxtRate.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("RATE", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("RATE", (TxtRate.Text));
                            }
                            if (TxtCustDis.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("DIS_PER", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("DIS_PER", (TxtCustDis.Text));
                            }
                            if (TxtCustDisRate.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("DIS_RATE", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("DIS_RATE", (TxtCustDisRate.Text));
                            }
                            if (TxtDealerDis.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("DEL_PER", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("DEL_PER", (TxtDealerDis.Text));
                            }
                            if (TxtDealerRate.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("DEL_RATE", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("DEL_RATE", (TxtDealerRate.Text));
                            }
                            if (TxtMaxDis.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("MAX_PER", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("MAX_PER", (TxtMaxDis.Text));
                            }
                            if (TxtMaxRate.Text == string.Empty)
                            {
                                HandleDetail2.SetAttribute("MAX_RATE", ("0"));
                            }
                            else
                            {
                                HandleDetail2.SetAttribute("MAX_RATE", (TxtMaxRate.Text));
                            }

                            HandleDetail2.SetAttribute("INS_USERID", (Session["USERNAME"].ToString()));
                            HandleDetail2.SetAttribute("INS_TERMINAL", (Session["PC"].ToString()));
                            HandleDetail2.SetAttribute("INS_DATE", (""));
                            HandleDetail2.SetAttribute("UPD_USERID", (Session["USERNAME"].ToString()));
                            HandleDetail2.SetAttribute("UPD_TERMINAL", (Session["PC"].ToString()));
                            HandleDetail2.SetAttribute("UPD_DATE", (""));
                            root1.AppendChild(HandleDetail2);
                        }
                    }
                    #endregion

                    string str = STOCK_PRICE_MASLogicLayer.InsertSTOCK_PRICE_MASDetail(insert, validation.RSC(XDoc1.OuterXml));

                    if (str.Length <= 8)
                    {
                        lblmsg.Text      = "STOCK PRICE MASTER SAVE SUCCESSFULLY.";
                        lblmsg.ForeColor = Color.Green;
                        //clear();
                        //FillStockPriceMasterGrid();
                        //UserRights();

                        #region ViewDetail

                        #region SET TEXT ON VIEW
                        clear();
                        DataSet   ds    = STOCK_PRICE_MASLogicLayer.GetAllIDWiseSTOCK_PRICE_MASDetail(str);
                        DataTable dt    = ds.Tables[0];
                        DataTable dtBal = ds.Tables[1];
                        if (dt.Rows.Count > 0)
                        {
                            DivEntry.Visible = true;
                            DivView.Visible  = false;

                            HfCompCode.Value = dt.Rows[0]["COMP_CODE"].ToString();
                            HfSrNo.Value     = dt.Rows[0]["SRNO"].ToString();
                            // HfStockCode.Value= dt.Rows[0][""].ToString();
                            TxtFromDate.Text        = Convert.ToDateTime(dt.Rows[0]["FRDT"].ToString()).ToString("dd-MM-yyyy");
                            TxtToDate.Text          = Convert.ToDateTime(dt.Rows[0]["TODT"].ToString()).ToString("dd-MM-yyyy");
                            TxtRemark.Text          = dt.Rows[0]["REMARK"].ToString();
                            DdlActive.SelectedValue = dt.Rows[0]["ACTIVE"].ToString();

                            if (dtBal.Rows.Count > 0)
                            {
                                GvStockPriceDet.DataSource = dtBal;
                                GvStockPriceDet.DataBind();
                            }
                        }
                        #endregion

                        ControllerDisable();
                        btnSave.Visible    = false;
                        Btncalldel.Visible = false;
                        BtncallUpd.Visible = false;
                        UserRights();
                        GvStockPriceDet.Enabled = false;
                        // GvStockPriceDet.Visible = true;
                        DivStockGrid.Style.Add("Display", "block");
                        FillStockPriceMasterGrid(Session["COMP_CODE"].ToString());
                        #endregion
                    }
                    else if (str.Contains("Already") || str.Contains("PRIMARY KEY"))
                    {
                        lblmsg.Text      = "STOCK PRICE MASTER ALREADY EXIST.";
                        lblmsg.ForeColor = Color.Red;
                    }
                    else
                    {
                        lblmsg.Text      = "ERROR : STOCK PRICE MASTER NOT SAVED";
                        lblmsg.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void GvStockPriceMaster_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Deletea")
                {
                    #region DELETE
                    clear();
                    DataSet   ds    = STOCK_PRICE_MASLogicLayer.GetAllIDWiseSTOCK_PRICE_MASDetail(e.CommandArgument.ToString());
                    DataTable dt    = ds.Tables[0];
                    DataTable dtBal = ds.Tables[1];
                    if (dt.Rows.Count > 0)
                    {
                        DivEntry.Visible = true;
                        DivView.Visible  = false;

                        HfCompCode.Value = dt.Rows[0]["COMP_CODE"].ToString();
                        HfSrNo.Value     = dt.Rows[0]["SRNO"].ToString();
                        // HfStockCode.Value= dt.Rows[0][""].ToString();
                        TxtFromDate.Text        = Convert.ToDateTime(dt.Rows[0]["FRDT"].ToString()).ToString("dd-MM-yyyy");
                        TxtToDate.Text          = Convert.ToDateTime(dt.Rows[0]["TODT"].ToString()).ToString("dd-MM-yyyy");
                        TxtRemark.Text          = dt.Rows[0]["REMARK"].ToString();
                        DdlActive.SelectedValue = dt.Rows[0]["ACTIVE"].ToString();

                        if (dtBal.Rows.Count > 0)
                        {
                            GvStockPriceDet.DataSource = dtBal;
                            GvStockPriceDet.DataBind();
                        }
                    }
                    btnSave.Visible    = false;
                    btnDelete.Visible  = true;
                    Btncalldel.Visible = true;
                    BtncallUpd.Visible = false;
                    ControllerDisable();
                    GvStockPriceDet.Enabled = false;
                    //GvStockPriceDet.Visible = true;
                    DivStockGrid.Style.Add("Display", "block");
                    #endregion
                }


                if (e.CommandName == "Edita")
                {
                    #region EDIT
                    clear();
                    DataSet   ds    = STOCK_PRICE_MASLogicLayer.GetAllIDWiseSTOCK_PRICE_MASDetail(e.CommandArgument.ToString());
                    DataTable dt    = ds.Tables[0];
                    DataTable dtBal = ds.Tables[1];
                    if (dt.Rows.Count > 0)
                    {
                        DivEntry.Visible = true;
                        DivView.Visible  = false;

                        HfCompCode.Value = dt.Rows[0]["COMP_CODE"].ToString();
                        HfSrNo.Value     = dt.Rows[0]["SRNO"].ToString();
                        // HfStockCode.Value= dt.Rows[0][""].ToString();
                        TxtFromDate.Text        = Convert.ToDateTime(dt.Rows[0]["FRDT"].ToString()).ToString("dd-MM-yyyy");
                        TxtToDate.Text          = Convert.ToDateTime(dt.Rows[0]["TODT"].ToString()).ToString("dd-MM-yyyy");
                        TxtRemark.Text          = dt.Rows[0]["REMARK"].ToString();
                        DdlActive.SelectedValue = dt.Rows[0]["ACTIVE"].ToString();

                        if (dtBal.Rows.Count > 0)
                        {
                            GvStockPriceDet.DataSource = dtBal;
                            GvStockPriceDet.DataBind();
                        }

                        BtncallUpd.Text = "UPDATE";

                        #endregion
                    }
                    #region CHECK UPDATE RIGHTS
                    if (Session["UPDATE"] != null)
                    {
                        if (Session["UPDATE"].ToString() == "Y")
                        {
                            ControllerEnable();
                        }
                        else
                        {
                            ControllerDisable();
                        }
                    }
                    #endregion
                    Btncalldel.Visible = false;
                    BtncallUpd.Visible = true;
                    btnSave.Visible    = true;
                    UserRights();
                    GvStockPriceDet.Enabled = true;
                    //GvStockPriceDet.Visible = true;
                    DivStockGrid.Style.Add("Display", "block");
                }

                if (e.CommandName == "Viewa")
                {
                    #region SET TEXT ON VIEW
                    clear();
                    DataSet   ds    = STOCK_PRICE_MASLogicLayer.GetAllIDWiseSTOCK_PRICE_MASDetail(e.CommandArgument.ToString());
                    DataTable dt    = ds.Tables[0];
                    DataTable dtBal = ds.Tables[1];
                    if (dt.Rows.Count > 0)
                    {
                        DivEntry.Visible = true;
                        DivView.Visible  = false;

                        HfCompCode.Value = dt.Rows[0]["COMP_CODE"].ToString();
                        HfSrNo.Value     = dt.Rows[0]["SRNO"].ToString();
                        // HfStockCode.Value= dt.Rows[0][""].ToString();
                        TxtFromDate.Text        = Convert.ToDateTime(dt.Rows[0]["FRDT"].ToString()).ToString("dd-MM-yyyy");
                        TxtToDate.Text          = Convert.ToDateTime(dt.Rows[0]["TODT"].ToString()).ToString("dd-MM-yyyy");
                        TxtRemark.Text          = dt.Rows[0]["REMARK"].ToString();
                        DdlActive.SelectedValue = dt.Rows[0]["ACTIVE"].ToString();

                        if (dtBal.Rows.Count > 0)
                        {
                            GvStockPriceDet.DataSource = dtBal;
                            GvStockPriceDet.DataBind();
                        }
                    }
                    #endregion
                    ControllerDisable();
                    btnSave.Visible    = false;
                    Btncalldel.Visible = false;
                    BtncallUpd.Visible = false;
                    UserRights();
                    GvStockPriceDet.Enabled = false;
                    // GvStockPriceDet.Visible = true;
                    DivStockGrid.Style.Add("Display", "block");
                }
            }


            catch (Exception ex)
            {
                lblmsg.Text = ex.Message.ToString();
            }
        }