using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;
using System.Data.OleDb;

namespace SUNWEB
{
    public partial class UserRights : System.Web.UI.Page
    {
        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 S = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        DateTime MonthstartDt;
        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        DataTable DtSubBranch = new DataTable();
        MySqlCommand Cmdv = new MySqlCommand();
        // User Variables 
        string cd_id = string.Empty;
        string UserId = string.Empty;
        string UserName = string.Empty;
        string PortalTypeId = string.Empty;
        string cd_Id = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string IsmultiBranch = string.Empty;
        
        string IsSuperAdmin = string.Empty;
        string ActiveCallvalue = string.Empty;
        // License Variables ..
        string PID = string.Empty;
        string Page_Acess = string.Empty;
        string Read_Acess = string.Empty;
        string Write_Acess = string.Empty;
        string Isdowld_Acess = string.Empty;
        string Rcd_id = string.Empty;
        string RPortalid = string.Empty;
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string L_MaxUserCnt = string.Empty;

        string L_RetailPayment = string.Empty;
        string Branchidval = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;
        string cm_id = string.Empty;
        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string ISHQ = string.Empty;
        DataSet DS = new DataSet();
        string SingleBranchName = string.Empty;
        // Page Rights Value
        // Page Rights Variables 
        string Pd_id = string.Empty;
        string Pid = string.Empty;
        string PageName = string.Empty;
        string PageAcess = string.Empty;
        string Readvalue = string.Empty;
        string Writevalue = string.Empty;
        string DLValue = string.Empty;

        string Cd_hq = string.Empty;

        // Read Variables 
        string Rsb_Id = string.Empty;
        string Rcm_id = string.Empty;
        
        string Rcd_BranchId = string.Empty;
        string RUserid = string.Empty;
        string RMultiAcess = string.Empty;
        string DataChk = string.Empty;
        string GCmid = string.Empty;
        


        string Vw_Chk = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "10";    // This is for   Rights Page  

            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (!string.IsNullOrEmpty(Session["UserId"].ToString()))
            {
                string GetCmidQuery = "select cm_id from  {0}.usermaster where userid=@userid;";
                string Command = string.Format(GetCmidQuery, "portaldb");

                MySqlParameter[] parameters =
                       {
                     new MySqlParameter("@userid",Session["UserId"].ToString()),

                    };
                GCmid = ObjMain.NewExecuteScalar(Command, parameters);

                hidCmid.Value = GCmid;

            }
            else

            {
                Response.Redirect("Login.aspx");
            }
              
            

            if (!IsPostBack)
            {
                lblLicsenceType.Visible = false;
            }
            MonthstartDt = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
           
            if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == "") && ((Convert.ToBoolean(Session["IsMultiBranch"])) == true))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();

                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "Select Branch")
                    {
                        ActiveCall();
                    }
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                }
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                //LoadInventoryListGrid(cd_BranchAlias, txtStartDt.Text, txtEndDt.Text);
            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName();
                    ActiveCall();
                }
            }
            else if (((Session["PortalTypeId"].ToString() != "") || (Convert.ToBoolean(Session["IsSuperAdmin"])) == false) && ((Convert.ToBoolean(Session["IsMultiBranch"])) == false))
            {
                cmbBranches.Enabled = false;
                cmbBranches.Visible = false;

                Cm_id = ObjSel.GETcmid(Session["Cd_Id"].ToString());  // Get cm_id
                HQ = ObjSel.Get_HQ(Cm_id);     // Get the HQ Value
                Curr_BranchFullName = ObjSel.GetBranchFullName(HQ, Session["Branch_HQ"].ToString());
                lblBranchAliasName.ForeColor = System.Drawing.Color.DodgerBlue;
                lblBranchAliasName.Text = "Branch Name" + "&nbsp;" + "-" + "&nbsp;" + Curr_BranchFullName + "&nbsp; ";

                cd_BranchAlias = Session["Branch_HQ"].ToString();
                Datapath = Session["DataPath"].ToString();
                PortalTypeId = Session["PortalTypeId"].ToString();

                DT = ObjUserBL.GetPageRightsDetailsPid(Session["UserId"].ToString(), PID);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }
                if ((Session["L_RetailPayment"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    // lblMsgTotalSelection.Text = "Item Sale  Between" + "&nbsp;&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  and  &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {
                            

                            if (cmbBranches.Visible == false)
                            {
                                //  BindGridview(Session["Branch_HQ"].ToString(), txtStartDt.Text, txtEndDt.Text);
                            }

                        }


                    }

                }

            }
            else
            {

            }


        }


        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
            ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
            if (ActiveCallvalue == "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
            }


            ISHQ = ObjMain.ExecuteScalar("select BranchName from " + ActiveCallvalue + ".branches where Branchname='" + ActiveCallvalue + "' and branchID=1;");  //now 

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
                UPDATEBRANCH(ActiveCallvalue);
            }

            cd_BranchAlias = ActiveCallvalue;
            Curr_BranchFullName = ObjUserBL.GetBranchCurr_BranchVal(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), cd_BranchAlias);
            Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Curr_BranchFullName);  // This will Return BranchName( Actual Database Name )
            cmbBranches.Items.FindByText(Curr_BranchFullName).Selected = true;
            Branchidval = ObjUserBL.GetBranchIDbyName(ActiveCallvalue, ActiveCallvalue);
            if (Branchidval != "")
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Session["Fistbranch"].ToString());

            }
            if (DT.Rows.Count > 0)
            {
                Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                Session["Page_Acess"] = Page_Acess;
                Session["Read_Acess"] = Read_Acess;
                Session["Write_Acess"] = Write_Acess;
                Session["Isdowld_Acess"] = Isdowld_Acess;
            }
            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + ActiveCallvalue + "';");
            RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
            DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);
            if (DtLicense.Rows.Count > 0)
            {


                L_RetailPayment = DtLicense.Rows[0]["RETAILPAYMENT"].ToString();
                Session["L_RetailPayment"] = L_RetailPayment;
            }
            if (Session["L_RetailPayment"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {
                Response.Redirect("DeniedPage.aspx");
            }
            else
            {

                //btnExcel.Enabled = true;
                //lblMsgTotalSelection.Text = "Total Sale Selection Between" + "&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                cd_BranchAlias = cmbBranches.SelectedItem.Text;

                if (!IsPostBack)
                {
                    LoadCompany();
                    LoadUsersList();
                    LoadPageList();
                    btnUpdate.Visible = false;
                    lblMultipleRightsLABEL.Visible = false;
                    lblLicsenceType.Visible = false;

                }

               
                // Calling Methods 
                System.Threading.Thread.Sleep(2000);

            }

        }

        public void LoadCompany()
        {
            cmbCompanyName.DataSource = ObjBL.GetCompanyList(hidCmid.Value);

            cmbCompanyName.DataTextField = "cm_name";
            cmbCompanyName.DataValueField = "cm_id";
            cmbCompanyName.DataBind();

        }
        public void LoadBranchName()
        {
            DtNormalUser = ObjUserBL.GetBranchOnLoginValues8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtNormalUser.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtNormalUser;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void LoadBranchName4SuperAdmin()
        {
            DtSuperAdmin = ObjUserBL.GetBranchOnSuperAdmin8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtSuperAdmin.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtSuperAdmin;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }
        }
        public void UPDATEBRANCH(string ActiveCallvalue)
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + ActiveCallvalue + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);
            }
            else
            {
                Response.Redirect("Login.aspx");

            }

        }


      
        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
        }


        public void LoadUsersList()
        { 
         DT = ObjUserBL.GetUserListViaCm_id(hidCmid.Value);
         cmbUser.DataSource = DT;
         cmbUser.DataTextField = "UserName";
         cmbUser.DataValueField = "UserId";
         cmbUser.DataBind();
         cmbUser.Items.Insert(0, "Select Users");
        
        }



        protected void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            // To refresh the Page At every Time:
            //Page.Response.Redirect(Page.Request.Url.ToString(), true);

            if (cmbUser.SelectedIndex == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Select User for Rights Permission ');", true);
            }
            else
            {

                // Existing code
                //  IsmultiBranch = ObjMain.ExecuteScalar("select IsMultiBranch from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");

                string Query1 = "select IsMultiBranch from {0}.Usermaster  where UserId=@UserId;";
                string command1 = string.Format(Query1, "portaldb");

                 MySqlParameter[] parameters1 =
                   {
                     new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                   };

                IsmultiBranch = ObjMain.NewExecuteScalar(command1, parameters1);

              




                // Existing code
                // IsSuperAdmin = ObjMain.ExecuteScalar("select Issuperadmin from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");

                string Query2 = "select Issuperadmin from {0}.Usermaster  where UserId=@UserId;";
                string command2 = string.Format(Query2, "portaldb");

                MySqlParameter[] parameters2 =
                  {
                     new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                   };

                IsSuperAdmin = ObjMain.NewExecuteScalar(command2, parameters2);




                if ((Convert.ToBoolean(IsmultiBranch) != true) && (Convert.ToBoolean(IsSuperAdmin) != true))
                {
                    DT = ObjUserBL.GetUserRightsDataByUserId(cmbUser.SelectedItem.Value);
                    if (DT.Rows.Count > 0)
                    {

                        SingleBranchName = DT.Rows[0]["BranchAlias"].ToString();

                        // Existing Coide 
                        //cm_id = ObjMain.ExecuteScalar("select  cm_id from  portaldb.companydetails  where cd_branchalias='" + SingleBranchName + "' ");

                        string Query3 = "select  cm_id from  {0}.companydetails  where cd_branchalias=@cd_branchalias; ";
                        string command3 = string.Format(Query3, "portaldb");
                        MySqlParameter[] parameters3 =
                         {
                         new MySqlParameter("@cd_branchalias",SingleBranchName),
                         };
                        cm_id = ObjMain.NewExecuteScalar(command3, parameters3);



                       // Exisiting Code 
                       // BranchFullName = ObjMain.ExecuteScalar("select cd_branchname from  portaldb.companydetails  where cm_id=" + cm_id + " and  cd_BranchAlias='" + SingleBranchName + "';");

                        string Query4 = "select cd_branchname from  {0}.companydetails  where cm_id=@cm_id and  cd_BranchAlias=@cd_BranchAlias;";
                        string Command4 = string.Format(Query4, "portaldb");

                        MySqlParameter[] parameters4 =
                        {
                         new MySqlParameter("@cm_id",cm_id),
                         new MySqlParameter("@cd_BranchAlias",SingleBranchName),
                        };
                        BranchFullName = ObjMain.NewExecuteScalar(Command4, parameters4);


                        btnSubmit.Visible = false;
                        btnUpdate.Visible = true;
                        CmbBranchList.Visible = false;
                        lblMultiBranchMsg.Visible = true;
                        lblMultipleRightsLABEL.Visible = false;
                        lbl1.Visible = false;
                       lblMultiBranchMsg.Text = " | " + " USER TYPE" + " -" + "<b>" + "&nbsp;&nbsp;" + " Single Branch Access  " + "</b> " + "&nbsp;&nbsp;" + "|" + " &nbsp;&nbsp;" + "BRANCH NAME" + " :<b> " + BranchFullName + "</b> " + " | ";

                        LoadPageList();
                        grdChkMultiBranch.Visible = false;
                        DT = ObjUserBL.GetUserRightsDataByUserId(cmbUser.SelectedItem.Value);
                        grdUserRights.DataSource = DT;
                        grdUserRights.DataBind();
                        ReadUserRightsDataForUser();
                        
                    }
                    else
                    {
                        btnSubmit.Visible = true;
                        btnUpdate.Visible = false;
                        lblMultiBranchMsg.Visible = false;
                        LoadPageList();


                    }

                }
                else if ((Convert.ToBoolean(IsmultiBranch) == true) || (Convert.ToBoolean(IsSuperAdmin) == true))
                {

                    CmbBranchList.Visible = true;
                    lblMultiBranchMsg.Visible = false;
                    lbl1.Visible = true;
                    LoadSubCompList();
                }
                else
                {

                }
            }
        }

        public void LoadPageList()
        {
            DataTable DtPageRights = new DataTable();

            DtPageRights = ObjUserBL.GetPageRightsList();
            grdUserRights.DataSource = DtPageRights;
            grdUserRights.DataBind();

        }

        public void LoadSubCompList()
        {
            // Existing Code 
            //Cd_hq = ObjMain.ExecuteScalar("select  cd_BranchAlias from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");

            string Query5 = "select  cd_BranchAlias from {0}.Usermaster  where UserId=@UserId;";
            string Command5 = string.Format(Query5, "portaldb");
            MySqlParameter[] parameters5 =
                   {
                     new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                     
                    };

            Cd_hq = ObjMain.NewExecuteScalar(Command5, parameters5);


            // Exiting Code 
           // cd_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");
            string Query6 = "select  cd_id  from {0}.Usermaster  where UserId=@UserId; ";
            string Command6 = string.Format(Query6, "portaldb");
            MySqlParameter[] parameters6 =
                  {
                     new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),

                    };
            cd_id = ObjMain.NewExecuteScalar(Command6, parameters6);


            // Existing Code 
            //cm_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.companydetails  where cd_id=" + cd_id + " ");
            string Query7 = "select  cd_id  from {0}.companydetails  where cd_id=@cd_id; ";
            string Command7 = string.Format(Query7, "portaldb");

            MySqlParameter[] parameters7 =
                 {
                     new MySqlParameter("@cd_id",cd_id),

                 };

            cm_id = ObjMain.NewExecuteScalar(Command7, parameters7);
            // Existing Code 
            //DtSubBranch = ObjMain.GetData_DT("select BranchID, Branchfullname  branches from {0}.branches  where BranchId !=1;");

            string Query8 = "select BranchID, Branchfullname  branches from {0}.branches  where BranchId !=1;";
            string Command8 = string.Format(Query8, Cd_hq);
            DtSubBranch = ObjMain.GetData_DT(Command8);

            CmbBranchList.DataSource = DtSubBranch;
            CmbBranchList.DataTextField = "branches";
            CmbBranchList.DataValueField = "BranchID";
            CmbBranchList.DataBind();
            CmbBranchList.Items.Insert(0, "Select Branch");


        }



        public void ReadUserRightsDataForUser()
        {

            DT = ObjUserBL.GetUserRightsDataByUserId(cmbUser.SelectedItem.Value);

            for (int i = 0; i < DT.Rows.Count; i++)
            {

                DataRow dr = DT.Rows[i];

                Pd_id = dr["Pd_id"].ToString();
                Pid = dr["Pid"].ToString();
                PageName = dr["PageName"].ToString();
                PageAcess = dr["Acess_Status"].ToString();
                Readvalue = dr["Read_Access"].ToString();
                Writevalue = dr["Write_Access"].ToString();
                DLValue = dr["IDwld_Access"].ToString();


                CheckBox chkPageAccess = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkPageAccess");
                CheckBox ChkReadvalue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkRead");
                CheckBox ChkWritevalue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkWrite");
                CheckBox ChkDLValue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkDownload");


                if (PageAcess.Equals("Y") == true)
                {
                    chkPageAccess.Checked = true;

                }
                else
                {
                    chkPageAccess.Checked = false;
                }

                if (Readvalue.Equals("Y") == true)
                {
                    ChkReadvalue.Checked = true;
                }
                else
                {
                    ChkReadvalue.Checked = false;
                }
                if (Writevalue.Equals("Y") == true)
                {
                    ChkWritevalue.Checked = true;
                }
                else
                {
                    ChkWritevalue.Checked = false;
                }
                if (DLValue.Equals("Y") == true)
                {
                    ChkDLValue.Checked = true;

                }
                else
                {
                    ChkDLValue.Checked = false;
                }

            }

        }
        protected void grdUserRights_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string sRetVal = string.Empty;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 Label lblPID = (e.Row.FindControl("lblPid") as Label);
                 Label lblPageName = (e.Row.FindControl("lblPageName") as Label);
                 CheckBox RChkboxPageAccess = (e.Row.FindControl("chkPageAccess") as CheckBox);
                 CheckBox RChkboxRead = (e.Row.FindControl("chkRead") as CheckBox);
                 CheckBox RChkboxWrite = (e.Row.FindControl("chkWrite") as CheckBox);
                 CheckBox RChkDownload = (e.Row.FindControl("chkDownload") as CheckBox);

                // Get The License Details 
                // Wether it Basic or Pro , ProPlus,EnterPrice ,EnterPrice+ 
                if(CmbBranchList.SelectedIndex!=0)
                {
                    // Get the cd_id Value
                    //select  cd_id from companydetails where cm_id=49 and cd_branchid=2;
                    lblLicsenceType.Visible = true;
                    string Query1 = "select  cd_id from {0}.companydetails where cm_id=@cm_id and cd_branchid=@cd_branchid;";
                    string command1 = string.Format(Query1, "portaldb");
                    MySqlParameter[] parameters1 =
                    {
                     new MySqlParameter("@cm_id",hidCmid.Value),
                     new MySqlParameter("@cd_branchid",CmbBranchList.SelectedValue),
                    };
                    string Read_cd_id = ObjMain.NewExecuteScalar(command1, parameters1);


                    // Get the PortalTypeid which means License Type 
                    //select portaltypeid from invoicedetails where cm_id=49 and cd_id=137;   
                    string Query2 = "select portaltypeid from {0}.invoicedetails where cm_id=@cm_id and cd_id=@cd_id;";
                    string Command2 = string.Format(Query2, "portaldb");

                    MySqlParameter[] parameters2 =
                    {
                     new MySqlParameter("@cm_id",hidCmid.Value),
                     new MySqlParameter("@cd_id",Read_cd_id),
                    };

                    string PortaldTypeId = ObjMain.NewExecuteScalar(Command2, parameters2);


                    // Load the License Type 
                    DataTable DtResult1 = new DataTable();
                    if (PortaldTypeId == "1")
                    {
                        lblLicsenceValue.Text = "BASIC";
                        string GetPagewithLiceseDetails = "select * from {0}.portaltypechild where Portaltypeid=1;";
                        string command3 = string.Format(GetPagewithLiceseDetails, "Portaldb");
                        DtResult1 = ObjMain.GetData_DT(command3);
                    }
                    else if(PortaldTypeId == "2")
                    {
                        lblLicsenceValue.Text = "PRO";
                        string GetPagewithLiceseDetails = "select * from {0}.portaltypechild where Portaltypeid=2;";
                        string command3 = string.Format(GetPagewithLiceseDetails, "Portaldb");
                        DtResult1 = ObjMain.GetData_DT(command3);

                    }
                    else if (PortaldTypeId == "3")
                    {
                        lblLicsenceValue.Text = "PRO+";
                        string GetPagewithLiceseDetails = "select * from {0}.portaltypechild where Portaltypeid=3;";
                        string command3 = string.Format(GetPagewithLiceseDetails, "Portaldb");
                        DtResult1 = ObjMain.GetData_DT(command3);

                    }
                    else if (PortaldTypeId == "4")
                    {
                        lblLicsenceValue.Text = "ENTERPRISE";
                        string GetPagewithLiceseDetails = "select * from {0}.portaltypechild where Portaltypeid=4;";
                        string command3 = string.Format(GetPagewithLiceseDetails, "Portaldb");
                        DtResult1 = ObjMain.GetData_DT(command3);

                    }

                    else if (PortaldTypeId == "5")
                    {
                        lblLicsenceValue.Text = "ENTERPRISE+";
                        string GetPagewithLiceseDetails = "select * from {0}.portaltypechild where Portaltypeid=5;";
                        string command3 = string.Format(GetPagewithLiceseDetails, "Portaldb");
                        DtResult1 = ObjMain.GetData_DT(command3);

                    }

                    if (DtResult1.Rows.Count > 0)
                        {

                            string DashBoard = DtResult1.Rows[0]["RETAILDASHBOARD"].ToString();
                            string SummarySales = DtResult1.Rows[0]["SUMMARYSALES"].ToString();
                            string Itemsales = DtResult1.Rows[0]["ITEMSALE"].ToString();
                            string TaxAnalysis = DtResult1.Rows[0]["TAXANALYSIS"].ToString();
                            string TopSale = DtResult1.Rows[0]["TOPSALE"].ToString();

                            string MonthlySale = DtResult1.Rows[0]["MONTHLYSALE"].ToString();
                            string HourlySale = DtResult1.Rows[0]["HOURLYSALE"].ToString();
                            string GroupSale = DtResult1.Rows[0]["GROUPSALE"].ToString();
                            string UserCreation = DtResult1.Rows[0]["USERCREATION"].ToString();
                            string UserRights = DtResult1.Rows[0]["USERRIGHTS"].ToString();
                            string Stock = DtResult1.Rows[0]["STOCK"].ToString();
                            string Inventory = DtResult1.Rows[0]["INVENTORY"].ToString();
                            string condimentReport = DtResult1.Rows[0]["CONDIMENTREPORT"].ToString();
                            string UpdateBranch = DtResult1.Rows[0]["UPDATEBRANCH"].ToString();
                            string StockMovement = DtResult1.Rows[0]["STOCKMOVEMENT"].ToString();
                            string JournalReport = DtResult1.Rows[0]["JOURNALREPORT"].ToString();
                            string RAPOReport = DtResult1.Rows[0]["RAPOREPORT"].ToString(); 
                            string RetailPayment = DtResult1.Rows[0]["RETAILPAYMENT"].ToString();

                            string DetailstockReport = DtResult1.Rows[0]["DETAILSTOCKREPORT"].ToString();
                            string StockCollector = DtResult1.Rows[0]["STOCKCOLLECTOR"].ToString();
                            string CustomerMaster = DtResult1.Rows[0]["CUSTOMERMASTER"].ToString();
                            string MemberHistory = DtResult1.Rows[0]["MEMBERHISTORY"].ToString();
                            string PointHistory = DtResult1.Rows[0]["POINTSHISTORY"].ToString();
                            string InventoryPromotions = DtResult1.Rows[0]["INVENTORYPROMOTIONS"].ToString();
                            string OnlineKDS = DtResult1.Rows[0]["ONLINEKDS"].ToString();
                            string Condiment = DtResult1.Rows[0]["CONDIMENT"].ToString();
                            string CondimentGroup = DtResult1.Rows[0]["CONDIMENTGROUP"].ToString();
                            string Modifier = DtResult1.Rows[0]["MODIFIER"].ToString();
                            string CategoryCustomize = DtResult1.Rows[0]["CATEGORYCUSTOMIZE"].ToString();
                            string LandingCustomize = DtResult1.Rows[0]["LANDINGCUSTOMIZE"].ToString();
                            string OuletClosure = DtResult1.Rows[0]["OUTLETCLOSURE"].ToString();
                            string TakeAwaySetting = DtResult1.Rows[0]["TAKEAWAYSETTING"].ToString();
                            string SpecialDiscount = DtResult1.Rows[0]["SPECIALDISCOUNT"].ToString();
                            string PriceCaptionsetting = DtResult1.Rows[0]["PRICECAPTIONSETTING"].ToString();
                            string IorderlaProductTiming = DtResult1.Rows[0]["IORDERLAPRODUCTTIMING"].ToString();
                            string Layoutsetting = DtResult1.Rows[0]["LAYOUTSETTING"].ToString();
                            string UpsellingSetting = DtResult1.Rows[0]["UPSELLINGSETTING"].ToString();
                            string ConsumerBehavier = DtResult1.Rows[0]["CONSUMERBEHAVIOURANALYSIS"].ToString();
                            string ReorderReport = DtResult1.Rows[0]["REORDERREPORT"].ToString();
                            
                            // #1  DASHBOARD
                            if (lblPageName.Text.ToString().Trim() == "DASHBOARD")
                            {
                                if (DashBoard.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (DashBoard.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;
                                }


                            }

                            //# 2 SUMMARYSALES
                            if (lblPageName.Text.ToString().Trim() == "SUMMARYSALES")
                            {


                                if (SummarySales.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (SummarySales.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // #3 ITEMSALE
                            if (lblPageName.Text.ToString().Trim() == "ITEMSALE")
                            {


                                if (Itemsales.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Itemsales.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // #4 TAXANALYSIS  
                            if (lblPageName.Text.ToString().Trim() == "TAXANALYSIS")
                            {

                                if (TaxAnalysis.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (TaxAnalysis.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 5 	TOPSALE
                            if (lblPageName.Text.ToString().Trim() == "TOPSALE")
                            {


                                if (TopSale.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (TopSale.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 6.   MONTHLYSALE
                                                    

                               
                          if (lblPageName.Text.ToString().Trim() == "MONTHLYSALE")
                            {


                                if (MonthlySale.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (MonthlySale.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 7.   HOURLYSALE



                            if (lblPageName.Text.ToString().Trim() == "HOURLYSALE")
                            {


                                if (HourlySale.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (HourlySale.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 8.   GROUPSALE
                            if (lblPageName.Text.ToString().Trim() == "GROUPSALE")
                            {


                                if (GroupSale.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (GroupSale.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 9.   USERCREATION
                            if (lblPageName.Text.ToString().Trim() == "USERCREATION")
                            {


                                if (UserCreation.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (UserCreation.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 10.   USERRIGHTS
                            if (lblPageName.Text.ToString().Trim() == "USERRIGHTS")
                            {


                                if (UserRights.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (UserRights.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 11.   	STOCK
                            if (lblPageName.Text.ToString().Trim() == "STOCK")
                            {


                                if (Stock.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Stock.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 12.   	INVENTORY
                            if (lblPageName.Text.ToString().Trim() == "INVENTORY")
                            {


                                if (Inventory.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Inventory.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 13.   	CONDIMENTREPORT
                            if (lblPageName.Text.ToString().Trim() == "CONDIMENTREPORT")
                            {


                                if (condimentReport.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (condimentReport.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }






                            // # 14.   UPDATEBRANCH
                            if (lblPageName.Text.ToString().Trim() == "UPDATEBRANCH")
                            {


                                if (UpdateBranch.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (UpdateBranch.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 15.   	STOCKMOVEMENT
                            if (lblPageName.Text.ToString().Trim() == "STOCKMOVEMENT")
                            {


                                if (StockMovement.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (StockMovement.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 16.   	JOURNALREPORT
                            if (lblPageName.Text.ToString().Trim() == "JOURNALREPORT")
                            {


                                if (JournalReport.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (JournalReport.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 17.   	RAPOREPORT	
                            if (lblPageName.Text.ToString().Trim() == "RAPOREPORT")
                            {


                                if (RAPOReport.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (RAPOReport.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 18.   	RETAILPAYMENT	
                            if (lblPageName.Text.ToString().Trim() == "RETAILPAYMENT")
                            {


                                if (RetailPayment.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (RetailPayment.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            








                            // # 19.   DETAILSTOCKREPORT

                            if (lblPageName.Text.ToString().Trim() == "DETAILSTOCKREPORT")
                            {

                                
                            if (DetailstockReport.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (DetailstockReport.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 20.   STOCKCOLLECTOR

                            if (lblPageName.Text.ToString().Trim() == "STOCKCOLLECTOR")
                            {


                                if (StockCollector.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (StockCollector.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 21.   CUSTOMERMASTER

                            if (lblPageName.Text.ToString().Trim() == "CUSTOMERMASTER")
                            {


                                if (CustomerMaster.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (CustomerMaster.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 22.   MEMBERHISTORY

                            if (lblPageName.Text.ToString().Trim() == "MEMBERHISTORY")
                            {


                                if (MemberHistory.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (MemberHistory.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 23.   POINTSHISTORY

                            if (lblPageName.Text.ToString().Trim() == "POINTSHISTORY")
                            {


                                if (PointHistory.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (PointHistory.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 24.   INVENTORYPROMOTIONS

                            if (lblPageName.Text.ToString().Trim() == "INVENTORYPROMOTIONS")
                            {


                                if (InventoryPromotions.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (InventoryPromotions.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 25.   INVENTORYPROMOTIONS

                            if (lblPageName.Text.ToString().Trim() == "ONLINEKDS")
                            {


                                if (OnlineKDS.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (OnlineKDS.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 26.   CONDIMENT

                            if (lblPageName.Text.ToString().Trim() == "CONDIMENT")
                            {


                                if (Condiment.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Condiment.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 27.   CONDIMENT

                            if (lblPageName.Text.ToString().Trim() == "CONDIMENTGROUP")
                            {


                                if (CondimentGroup.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (CondimentGroup.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 28.   MODIFIER

                            if (lblPageName.Text.ToString().Trim() == "MODIFIER")
                            {


                                if (Modifier.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Modifier.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 29.   CATEGORYCUSTOMIZE

                            if (lblPageName.Text.ToString().Trim() == "CATEGORYCUSTOMIZE")
                            {


                                if (CategoryCustomize.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (CategoryCustomize.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 30.   LANDINGCUSTOMIZE

                            if (lblPageName.Text.ToString().Trim() == "LANDINGCUSTOMIZE")
                            {


                                if (LandingCustomize.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (LandingCustomize.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 31.   OUTLETCLOSURE

                            if (lblPageName.Text.ToString().Trim() == "OUTLETCLOSURE")
                            {


                                if (OuletClosure.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (OuletClosure.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }
                            // # 32.   OUTLETCLOSURE

                            if (lblPageName.Text.ToString().Trim() == "TAKEAWAYSETTING")
                            {


                                if (TakeAwaySetting.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (TakeAwaySetting.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }

                            // # 33.   OUTLETCLOSURE

                            if (lblPageName.Text.ToString().Trim() == "SPECIALDISCOUNT")
                            {


                                if (SpecialDiscount.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (SpecialDiscount.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 34.   PRICECAPTIONSETTING

                            if (lblPageName.Text.ToString().Trim() == "PRICECAPTIONSETTING")
                            {


                                if (PriceCaptionsetting.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (PriceCaptionsetting.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 35.   IORDERLAPRODUCTTIMING

                            if (lblPageName.Text.ToString().Trim() == "IORDERLAPRODUCTTIMING")
                            {


                                if (IorderlaProductTiming.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (IorderlaProductTiming.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 36.   LAYOUTSETTING

                            if (lblPageName.Text.ToString().Trim() == "LAYOUTSETTING")
                            {


                                if (Layoutsetting.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (Layoutsetting.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }


                            // # 37.   UPSELLINGSETTING

                            if (lblPageName.Text.ToString().Trim() == "UPSELLINGSETTING")
                            {


                                if (UpsellingSetting.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (UpsellingSetting.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 38.   CONSUMERBEHAVIOURANALYSIS

                            if (lblPageName.Text.ToString().Trim() == "CONSUMERBEHAVIOURANALYSIS")
                            {


                                if (ConsumerBehavier.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (ConsumerBehavier.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }



                            // # 39.   REORDERREPORT

                            if (lblPageName.Text.ToString().Trim() == "REORDERREPORT")
                            {


                                if (ReorderReport.Equals("Y"))
                                {

                                    RChkboxPageAccess.Enabled = true;
                                    RChkboxRead.Enabled = true;
                                    RChkboxWrite.Enabled = true;
                                    RChkDownload.Enabled = true;


                                }
                                else if (ReorderReport.Equals("N"))
                                {
                                    RChkboxPageAccess.Enabled = false;
                                    RChkboxRead.Enabled = false;
                                    RChkboxWrite.Enabled = false;
                                    RChkDownload.Enabled = false;



                                }
                            }






                        }
                        


                }

            }
        }

        protected void CmbBranchList_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (CmbBranchList.SelectedIndex == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Select Branch for Rights Permission ');", true);

            }
            else
            {

                if (cmbCompanyName.SelectedIndex != 0 || cmbUser.SelectedIndex != 0)
                {
                    // Remove :For testing 
                    LoadPageList();
                    LoadSubCompanyList();
                    CmbBranchList.Visible = true;
                    lblMultipleRightsLABEL.Visible = true;
                    lbl1.Visible = true;
                    DT = ObjUserBL.GetUserRightsDataByBranchID(cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);
                    if (DT.Rows.Count > 0)
                    {
                        grdUserRights.DataSource = DT;
                        grdUserRights.DataBind();
                        ReadUserRightsData();
                        grdChkMultiBranch.Visible = true;
                        Dt2 = ObjUserBL.GetSubCompDetailByUserIdndBranchid(Cd_hq, cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);
                        ReadUserRightsData();
                    }
                    else
                    {

                        btnSubmit.Visible = true;
                        btnUpdate.Visible = false;
                        LoadPageList();
                        LoadSubCompanyList();

                    }


                    Dt2 = ObjUserBL.GetSubCompDetailByUserIdndBranchid(Cd_hq, cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);
                    if (Dt2.Rows.Count > 0)
                    {
                        btnSubmit.Visible = false;
                        btnUpdate.Visible = true;
                        lblMultiBranchMsg.Visible = false;
                        grdChkMultiBranch.DataSource = Dt2;
                        grdChkMultiBranch.DataBind();
                        Dt2 = ObjUserBL.GetSubCompDetailByUserIdndBranchid(Cd_hq, cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);
                        ReadSubBranchData();

                    }
                    else
                    {
                        btnSubmit.Visible = true;
                        LoadPageList();
                        LoadSubCompanyAlter();

                    }

                }
                else
                {

                }


            //    AutoCreate();


            }

        }



        public void AutoCreate()
        {
            string Userid = string.Empty;
            string cd_BranchId = string.Empty;

            
                foreach (GridViewRow gvrow in grdUserRights.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label Pid_Val = gvrow.FindControl("lblPid") as Label;
                        Label lblcd_branchname_Value = gvrow.FindControl("lblPageName") as Label;
                        CheckBox ChkPageValue = gvrow.FindControl("chkPageAccess") as CheckBox;
                        CheckBox ChkReadValue = gvrow.FindControl("chkRead") as CheckBox;
                        CheckBox ChkWriteValue = gvrow.FindControl("chkWrite") as CheckBox;
                        CheckBox ChkDonloadValue = gvrow.FindControl("chkDownload") as CheckBox;
                        Userid = cmbUser.SelectedItem.Value;

                        ObjCustPL.Pid = Convert.ToInt32(Pid_Val.Text);
                        ObjCustPL.UserId = Userid;
                        if (ChkPageValue.Checked == true)
                        {

                            ObjCustPL.PageAccess = "Y";
                        }
                        else if (ChkPageValue.Checked == false)
                        {

                            ObjCustPL.PageAccess = "N";

                        }


                        if (ChkReadValue.Checked == true)
                        {

                            ObjCustPL.ReadAccess = "Y";
                        }
                        else if (ChkReadValue.Checked == false)
                        {

                            ObjCustPL.ReadAccess = "N";
                        }

                        if (ChkWriteValue.Checked == true)
                        {

                            ObjCustPL.WriteAccess = "Y";
                        }
                        else if (ChkWriteValue.Checked == false)
                        {

                            ObjCustPL.WriteAccess = "N";
                        }


                        if (ChkDonloadValue.Checked == true)
                        {

                            ObjCustPL.IsDownload = "Y";
                        }
                        else if (ChkDonloadValue.Checked == false)
                        {

                            ObjCustPL.IsDownload = "N";
                        }


                    if (CmbBranchList.Visible == true)
                    {

                       
                            // Add the Insert Logic Here..

                            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");
                            cd_BranchId = ObjMain.ExecuteScalar("select cd_branchid from portaldb.companydetails  where cd_id=" + Rcd_id + "");


                            string QryPd = "Insert into  portaldb.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access )Values(" + ObjCustPL.Pid + "," + cd_BranchId + " ," + Userid + ",'" + ObjCustPL.PageAccess + "','" + ObjCustPL.ReadAccess + "' ,'" + ObjCustPL.WriteAccess + "' ,'" + ObjCustPL.IsDownload + "' )";
                            // This is function for Inserting in Pagedetails Tables 
                            K = ObjMain.ExecuteNonQuery(QryPd);


                        



                    }
                    else
                    {
                                               
                            // Add the Insert Logic Here..
                            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");
                            cd_BranchId = ObjMain.ExecuteScalar("select cd_branchid from portaldb.companydetails  where cd_id=" + Rcd_id + "");
                            string QryPd = "Insert into  portaldb.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access )Values(" + ObjCustPL.Pid + "," + cd_BranchId + " ," + Userid + ",'" + ObjCustPL.PageAccess + "','" + ObjCustPL.ReadAccess + "' ,'" + ObjCustPL.WriteAccess + "' ,'" + ObjCustPL.IsDownload + "' )";
                            // This is function for Inserting in Pagedetails Tables 
                            K = ObjMain.ExecuteNonQuery(QryPd);


                    }



                 }
                }


                // Getting SubCompany's Details : 

                foreach (GridViewRow gvrow in grdChkMultiBranch.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label BranchId_Val = gvrow.FindControl("lblBranchID") as Label;
                        Label branchname_Value = gvrow.FindControl("lblBranchName") as Label;
                        CheckBox ChkMulBr = gvrow.FindControl("chkBranchPageAccess") as CheckBox;

                        if (ChkMulBr.Checked == true)
                        {
                            ObjCustPL.IsMultiBranch = "Y";

                        }
                        else if (ChkMulBr.Checked == false)
                        {
                            ObjCustPL.IsMultiBranch = "N";
                        }
                        string QryOnSubcomp = " Insert into  portaldb.subcompmaster(cm_id,cd_Branchid,userid, Multi_Access)values(" + Session["Cm_Id"].ToString() + "," + BranchId_Val.Text + "," + Userid + ",'" + ObjCustPL.IsMultiBranch + "' ) ";
                        L = ObjMain.ExecuteNonQuery(QryOnSubcomp);
                        // Add the Insert code Here 


                    
                }
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('User Rights has been Created For All Branches ');location.href='RightsGeneration.aspx '", true);
                //LoadCompListViaUserMaster();

            }



        }








        public void LoadSubCompanyAlter()
        {
            // Existing Code 
            //Cd_hq = ObjMain.ExecuteScalar("select  cd_BranchAlias from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");
            string Query9 = "select  cd_BranchAlias from {0}.Usermaster  where UserId=@UserId; ";
            string command9 = string.Format(Query9, "portaldb");
            MySqlParameter[] parameters9 =
                 {
                  new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                  };
            Cd_hq = ObjMain.NewExecuteScalar(command9, parameters9);


            // Existing Code 
           //cd_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");
            string Query10 = "select  cd_id  from {0}.Usermaster  where UserId=@UserId;";
            string Command10 = string.Format(Query10, "0");
            MySqlParameter[] parameters10 =
                 {
                  new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                  };

            cd_id = ObjMain.NewExecuteScalar(Command10, parameters10);


            // Existing Code 
            //cm_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.companydetails  where cd_id=" + cd_id + " ");
            string Query11 = "select  cd_id  from {0}.companydetails  where cd_id=@cd_id; ";
            string command11 = string.Format(Query11, "portaldb");
            MySqlParameter[] parameters11 =
                {
                  new MySqlParameter("@cd_id",cd_id),
                };
            cm_id = ObjMain.NewExecuteScalar(command11, parameters11);
            Session["Cm_Id"] = cm_id;
            // Existing Code 
           // DtSubBranch = ObjMain.GetData_DT("SELECT BranchID ,Branchfullname  branches from  " + Cd_hq + ".branches  where     BranchID NOT IN   (select cd_branchid from portaldb.subcompmaster WHERE  USERID=" + cmbUser.SelectedItem.Value + ") and branchid!=1;");
            string Query12 = "SELECT BranchID ,Branchfullname  branches from  {1}.branches  where     BranchID NOT IN   (select cd_branchid from {0}.subcompmaster WHERE  USERID=@USERID) and branchid!=1;";
            string command12 = string.Format(Query12, "portaldb", Cd_hq);
            MySqlParameter[] parameters12 =
               {
                  new MySqlParameter("@USERID",cmbUser.SelectedItem.Value),
               };
            DtSubBranch = ObjMain.NewGetData_DT(command12, parameters12);
            grdChkMultiBranch.DataSource = DtSubBranch;
            grdChkMultiBranch.DataBind();
        }

        public void LoadSubCompanyList()
        {

            // Existing Code 
           // Cd_hq = ObjMain.ExecuteScalar("select  cd_BranchAlias from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");

            string Query13 = "select  cd_BranchAlias from {0}.Usermaster  where UserId=@UserId;";
            string Command13 = string.Format(Query13, "portaldb");
            MySqlParameter[] parameters13 =
              {
                  new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
              };
            Cd_hq = ObjMain.NewExecuteScalar(Command13, parameters13);
            
            //cd_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + " ");

            string Query14 = "select  cd_id  from {0}.Usermaster  where UserId=@UserId;";
            string Command14 = string.Format(Query14, "portaldb");
            MySqlParameter[] parameters14 =
              {
                  new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
              };
            cd_id = ObjMain.NewExecuteScalar(Command14, parameters14);


            //cm_id = ObjMain.ExecuteScalar("select  cd_id  from portaldb.companydetails  where cd_id=" + cd_id + " ");
            string Query15 = "select  cd_id  from {0}.companydetails  where cd_id=@cd_id; ";
            string Command15 = string.Format(Query15, "portaldb");
            MySqlParameter[] parameters15 =
             {
                  new MySqlParameter("@cd_id",cd_id),
              };
            cm_id = ObjMain.NewExecuteScalar(Command15, parameters15);

            Session["Cm_Id"] = cm_id;

           // DtSubBranch = ObjMain.GetData_DT("select BranchID, Branchfullname  branches from " + Cd_hq + ".branches  where BranchId !=1");

            string Query16 = "select BranchID, Branchfullname  branches from {0}.branches  where BranchId !=1;";
            string command16 = string.Format(Query16, Cd_hq);
            DtSubBranch = ObjMain.GetData_DT(command16);
            grdChkMultiBranch.DataSource = DtSubBranch;
            grdChkMultiBranch.DataBind();
        }


        public void ReadSubBranchData()
        {

            Dt2 = ObjUserBL.GetSubCompDetailByUserIdndBranchid(Cd_hq, cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);
            //GridView1.DataSource = DT;
            //GridView1.DataBind();
            for (int i = 0; i < Dt2.Rows.Count; i++)
            {
                DataRow dr = Dt2.Rows[i];
                Rsb_Id = dr["sb_Id"].ToString();
                Rcm_id = dr["cm_id"].ToString();
                Rcd_BranchId = dr["cd_Branchid"].ToString();
                RUserid = dr["userid"].ToString();
                RMultiAcess = dr["Multi_Access"].ToString();
                CheckBox chkSubPageAccess = (CheckBox)grdChkMultiBranch.Rows[i].Cells[0].FindControl("chkBranchPageAccess");
                if (RMultiAcess.Equals("Y") == true)
                {
                    chkSubPageAccess.Checked = true;

                }
                else
                {
                    chkSubPageAccess.Checked = false;
                }

            }

        }


        public void ReadUserRightsData()
        {

            DT = ObjUserBL.GetUserRightsDataByBranchID(cmbUser.SelectedItem.Value, CmbBranchList.SelectedItem.Value);

            for (int i = 0; i < DT.Rows.Count; i++)
            {

                DataRow dr = DT.Rows[i];
                Pd_id = dr["Pd_id"].ToString();
                Pid = dr["Pid"].ToString();
                PageName = dr["PageName"].ToString();
                PageAcess = dr["Acess_Status"].ToString();
                Readvalue = dr["Read_Access"].ToString();
                Writevalue = dr["Write_Access"].ToString();
                DLValue = dr["IDwld_Access"].ToString();
                CheckBox chkPageAccess = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkPageAccess");
                CheckBox ChkReadvalue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkRead");
                CheckBox ChkWritevalue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkWrite");
                CheckBox ChkDLValue = (CheckBox)grdUserRights.Rows[i].Cells[0].FindControl("chkDownload");


                if (PageAcess.Equals("Y") == true)
                {
                    chkPageAccess.Checked = true;

                }
                else
                {
                    chkPageAccess.Checked = false;
                }

                if (Readvalue.Equals("Y") == true)
                {
                    ChkReadvalue.Checked = true;
                }
                else
                {
                    ChkReadvalue.Checked = false;
                }
                if (Writevalue.Equals("Y") == true)
                {
                    ChkWritevalue.Checked = true;
                }
                else
                {
                    ChkWritevalue.Checked = false;
                }
                if (DLValue.Equals("Y") == true)
                {
                    ChkDLValue.Checked = true;

                }
                else
                {
                    ChkDLValue.Checked = false;
                }

            }




        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string Userid = string.Empty;
            string cd_BranchId = string.Empty;


            if (cmbUser.SelectedIndex == 0 || CmbBranchList.SelectedIndex == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Select All Required Selection ')", true);

            }
            else
            {

                foreach (GridViewRow gvrow in grdUserRights.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label Pid_Val = gvrow.FindControl("lblPid") as Label;
                        Label lblcd_branchname_Value = gvrow.FindControl("lblPageName") as Label;
                        CheckBox ChkPageValue = gvrow.FindControl("chkPageAccess") as CheckBox;
                        CheckBox ChkReadValue = gvrow.FindControl("chkRead") as CheckBox;
                        CheckBox ChkWriteValue = gvrow.FindControl("chkWrite") as CheckBox;
                        CheckBox ChkDonloadValue = gvrow.FindControl("chkDownload") as CheckBox;
                        Userid = cmbUser.SelectedItem.Value;

                        ObjCustPL.Pid = Convert.ToInt32(Pid_Val.Text);
                        ObjCustPL.UserId = Userid;
                        if (ChkPageValue.Checked == true)
                        {

                            ObjCustPL.PageAccess = "Y";
                        }
                        else if (ChkPageValue.Checked == false)
                        {

                            ObjCustPL.PageAccess = "N";

                        }


                        if (ChkReadValue.Checked == true)
                        {

                            ObjCustPL.ReadAccess = "Y";
                        }
                        else if (ChkReadValue.Checked == false)
                        {

                            ObjCustPL.ReadAccess = "N";
                        }

                        if (ChkWriteValue.Checked == true)
                        {

                            ObjCustPL.WriteAccess = "Y";
                        }
                        else if (ChkWriteValue.Checked == false)
                        {

                            ObjCustPL.WriteAccess = "N";
                        }


                        if (ChkDonloadValue.Checked == true)
                        {

                            ObjCustPL.IsDownload = "Y";
                        }
                        else if (ChkDonloadValue.Checked == false)
                        {

                            ObjCustPL.IsDownload = "N";
                        }


                        if (CmbBranchList.Visible == true)
                        {


                            // Add the Insert Logic Here..
                            // Existing Code 
                            //Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");

                            string Query1 = "select cd_id from {0}.Usermaster  where UserId=@UserId;";
                            string Command1 = string.Format(Query1, "portaldb");

                            MySqlParameter[] parameters1 =
                             {
                             new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                             };
                            Rcd_id = ObjMain.NewExecuteScalar(Command1, parameters1);




                            // Existing Code 
                           // cd_BranchId = ObjMain.ExecuteScalar("select cd_branchid from portaldb.companydetails  where cd_id=" + Rcd_id + "");
                            string Query2 = "select cd_branchid from {0}.companydetails  where cd_id=@cd_id;";
                            string Command2 = string.Format(Query2, "portaldb");

                            MySqlParameter[] parameters2 =
                            {
                             new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                             };

                            cd_BranchId = ObjMain.NewExecuteScalar(Command2, parameters2);

                            // This is function for Inserting in Pagedetails Tables 
                            string QryPd = "Insert into  {0}.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access ) " +
                                           " Values(@Pid,@BranchId ,@Userid,@Acess_Status,@Read_Access, @Write_Access ,@IDwld_Access);";
                            

                            string command3 = string.Format(QryPd, "portaldb");

                            MySqlParameter[] parameters3 =
                                   {
                                     new MySqlParameter("@Pid",ObjCustPL.Pid),
                                     new MySqlParameter("@BranchId",cd_BranchId),
                                     new MySqlParameter("@Userid",Userid),
                                     new MySqlParameter("@Acess_Status",ObjCustPL.PageAccess),
                                     new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                     new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                     new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),
                                     
                                    };

                            K = ObjMain.ExecuteQuery(command3, parameters3);


                        }
                        else
                        {


                            // Add the Insert Logic Here..
                            // Existing Code 
                            //Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");

                            string Query4 = "select cd_id from {0}.Usermaster  where UserId=@UserId;";
                            string Command4 = string.Format(Query4, "portaldb");
                            MySqlParameter[] parameters4 =
                                 {
                                     new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                                 };
                            Rcd_id = ObjMain.NewExecuteScalar(Command4, parameters4);

                            // Existing Code 
                           // cd_BranchId = ObjMain.ExecuteScalar("select cd_branchid from portaldb.companydetails  where cd_id=" + Rcd_id + "");
                            string Query5 = "select cd_branchid from {0}.companydetails  where cd_id=@cd_id; ";
                            string Command5 = string.Format(Query5, "portaldb");
                            MySqlParameter[] parameters5 =
                               {
                                     new MySqlParameter("@cd_id",Rcd_id),
                               };
                            cd_BranchId = ObjMain.NewExecuteScalar(Command5, parameters5);

                            string QryPd = "Insert into  {0}.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access ) " +
                                " Values(@Pid,@BranchId ,@Userid,@Acess_Status,@Read_Access, @Write_Access ,@IDwld_Access );";
                            // This is function for Inserting in Pagedetails Tables 
                            string command6 = string.Format(QryPd, "portaldb");

                            MySqlParameter[] parameters6 =
                                   {
                                     new MySqlParameter("@Pid",ObjCustPL.Pid),
                                     new MySqlParameter("@BranchId",cd_BranchId),
                                     new MySqlParameter("@Userid",Userid),
                                     new MySqlParameter("@Acess_Status",ObjCustPL.PageAccess),
                                     new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                     new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                     new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),

                                    };

                            K = ObjMain.ExecuteQuery(command6, parameters6);




                        }






                    }
                }


                // Getting SubCompany's Details : 

                foreach (GridViewRow gvrow in grdChkMultiBranch.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label BranchId_Val = gvrow.FindControl("lblBranchID") as Label;
                        Label branchname_Value = gvrow.FindControl("lblBranchName") as Label;
                        CheckBox ChkMulBr = gvrow.FindControl("chkBranchPageAccess") as CheckBox;

                        if (ChkMulBr.Checked == true)
                        {
                            ObjCustPL.IsMultiBranch = "Y";

                        }
                        else if (ChkMulBr.Checked == false)
                        {
                            ObjCustPL.IsMultiBranch = "N";
                        }

                        // Get cd_id in Companydetails 

                        string QueryA = "select cd_id from {0}.companydetails where  cm_id=@cm_id  and  cd_hq=1;";
                        string commandA = string.Format(QueryA, "portaldb");
                        MySqlParameter[] parametersA =
                        {

                        new MySqlParameter("@cm_id", hidCmid.Value),
                        };
                        string cd_id_Value = ObjMain.NewExecuteScalar(commandA, parametersA);

                        string QryOnSubcomp = " Insert into  {0}.subcompmaster(cm_id,cd_Branchid,userid, Multi_Access) " +
                            " values(@cm_id,@cd_Branchid,@userid,Multi_Access);";
                        string Command7 = string.Format(QryOnSubcomp, "portaldb");

                        MySqlParameter[] parameters7 =
                                   {
                                     new MySqlParameter("@cm_id",cd_id_Value),
                                     new MySqlParameter("@cd_Branchid",BranchId_Val.Text),
                                     new MySqlParameter("@userid",Userid),
                                     new MySqlParameter("@Multi_Access",ObjCustPL.IsMultiBranch),
                                   
                                    };


                        L = ObjMain.ExecuteQuery(Command7,parameters7);
                        // Add the Insert code Here 


                    }
                }
                cmbUser.SelectedIndex = 0;
                CmbBranchList.SelectedIndex = -1;

                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('User Rights has been Generated ');", true);
                // LoadCompListViaUserMaster();



            }





        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string cd_BranchId = string.Empty;
            string Userid = string.Empty;


            if (cmbUser.SelectedIndex == 0 || CmbBranchList.SelectedIndex == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Select All Required Selection ')", true);

            }
            else
            {
                foreach (GridViewRow gvrow in grdUserRights.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {

                        Label Pid_Val = gvrow.FindControl("lblPid") as Label;
                        Label lblcd_branchname_Value = gvrow.FindControl("lblPageName") as Label;
                        CheckBox ChkPageValue = gvrow.FindControl("chkPageAccess") as CheckBox;
                        CheckBox ChkReadValue = gvrow.FindControl("chkRead") as CheckBox;
                        CheckBox ChkWriteValue = gvrow.FindControl("chkWrite") as CheckBox;
                        CheckBox ChkDonloadValue = gvrow.FindControl("chkDownload") as CheckBox;
                        Userid = cmbUser.SelectedItem.Value;

                        ObjCustPL.Pid = Convert.ToInt32(Pid_Val.Text);
                        ObjCustPL.UserId = Userid;
                        if (ChkPageValue.Checked == true)
                        {

                            ObjCustPL.PageAccess = "Y";
                        }
                        else if (ChkPageValue.Checked == false)
                        {

                            ObjCustPL.PageAccess = "N";

                        }


                        if (ChkReadValue.Checked == true)
                        {

                            ObjCustPL.ReadAccess = "Y";
                        }
                        else if (ChkReadValue.Checked == false)
                        {

                            ObjCustPL.ReadAccess = "N";
                        }

                        if (ChkWriteValue.Checked == true)
                        {

                            ObjCustPL.WriteAccess = "Y";
                        }
                        else if (ChkWriteValue.Checked == false)
                        {

                            ObjCustPL.WriteAccess = "N";
                        }


                        if (ChkDonloadValue.Checked == true)
                        {

                            ObjCustPL.IsDownload = "Y";
                        }
                        else if (ChkDonloadValue.Checked == false)
                        {

                            ObjCustPL.IsDownload = "N";
                        }


                        if (CmbBranchList.Visible == false)
                        {

                            // Existing Code 
                            //Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.Usermaster  where UserId=" + cmbUser.SelectedItem.Value + "");
                            string Query1 = "select cd_id from {0}.Usermaster  where UserId=@UserId;";
                            string Comand1 = string.Format(Query1, "portaldb");
                            MySqlParameter[] parameters1 =
                                       {
                                         new MySqlParameter("@UserId",cmbUser.SelectedItem.Value),
                                       };
                            Rcd_id = ObjMain.NewExecuteScalar(Comand1, parameters1);

                            // Existing Code 
                            //cd_BranchId = ObjMain.ExecuteScalar("select cd_branchid from portaldb.companydetails  where cd_id=" + Rcd_id + "");
                            string Query2 = "select cd_branchid from {0}.companydetails  where cd_id=@cd_id;";
                            string Command2 = string.Format(Query2, "portaldb");
                            MySqlParameter[] parameters2 =
                                    {
                                         new MySqlParameter("@cd_id",Rcd_id),
                                    };

                            cd_BranchId = ObjMain.NewExecuteScalar(Command2, parameters2);

                            // Existing Code 

                           // DataChk = ObjMain.ExecuteScalar("select Pid from portaldb.pagedetails where Pid=" + ObjCustPL.Pid + " AND Userid=" + Userid + "  and  BranchId=" + cd_BranchId + ";");
                            string Query3 = "select Pid from {0}.pagedetails where Pid=@Pid AND Userid=@Userid  and  BranchId=@BranchId;";
                            string Command3 = string.Format(Query3, "portaldb");
                            MySqlParameter[] parameters3 =
                                   {
                                         new MySqlParameter("@Pid",ObjCustPL.Pid),
                                         new MySqlParameter("@Userid",Userid),
                                         new MySqlParameter("@BranchId",cd_BranchId),
                                   };

                            DataChk = ObjMain.NewExecuteScalar(Command3, parameters3);


                            if (DataChk != "")
                            {

                                string QryUPT_Pd = "Update  {0}.pagedetails  set Acess_Status=@Acess_Status, Read_Access=@Read_Access, Write_Access=@Write_Access, IDwld_Access=@IDwld_Access  where Pid=@Pid AND Userid=@Userid and BranchId=@BranchId ;";
                                // This is function for Update If Record Exist in Pagedetails Tables 

                                string command4 = string.Format(QryUPT_Pd, "portaldb");

                                MySqlParameter[] parameters4 =
                                  {
                                         new MySqlParameter("@Acess_Status",ObjCustPL.PageAccess),
                                         new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                         new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                         new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),
                                         new MySqlParameter("@Pid",ObjCustPL.Pid),
                                         new MySqlParameter("@Userid",Userid),
                                         new MySqlParameter("@BranchId",cd_BranchId),


                                   };

                                K = ObjMain.ExecuteQuery(command4, parameters4);
                            }
                            else
                            {
                                string QryPd = "Insert into  {0}.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access )" +
                                    " Values(@Pid,@BranchId ,@Userid,@Acess_Status,@Read_Access ,@Write_Access ,@IDwld_Access );";
                                string command5 = string.Format(QryPd, "portaldb");



                                MySqlParameter[] parameters5 =
                                  {
                                         new MySqlParameter("@Pid",ObjCustPL.Pid),
                                         new MySqlParameter("@BranchId",cd_BranchId),
                                         new MySqlParameter("@Userid",Userid),
                                         new MySqlParameter("@Acess_Status", ObjCustPL.PageAccess),
                                         new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                         new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                         new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),


                                   };

                                K = ObjMain.ExecuteQuery(command5, parameters5);

                            }

                        }
                        else
                        {

                            // Add the Update Logic Here..

                            // Existing Code 

                            //DataChk = ObjMain.ExecuteScalar("select Pid from portaldb.pagedetails where Pid=" + ObjCustPL.Pid + " AND Userid=" + Userid + "  and  BranchId=" + CmbBranchList.SelectedItem.Value + ";");
                            string Query6 = "select Pid from {0}.pagedetails where Pid=@Pid AND Userid=@Userid  and  BranchId=@BranchId;";
                            string Command6 = string.Format(Query6, "portaldb");

                            MySqlParameter[] parameters6 =
                               {
                                 new MySqlParameter("@Pid",ObjCustPL.Pid),
                                 new MySqlParameter("@Userid",Userid),
                                 new MySqlParameter("@BranchId",CmbBranchList.SelectedItem.Value),
                                 
                                };
                            DataChk = ObjMain.NewExecuteScalar(Command6, parameters6);


                            if (DataChk != "")
                            {
                                // This is function for Update If Record Exist in Pagedetails Tables 
                                string QryUPT_Pd = "Update  {0}.pagedetails  set Acess_Status=@Acess_Status, Read_Access=@Read_Access, Write_Access=@Write_Access, IDwld_Access=@IDwld_Access  where Pid=@Pid AND Userid=@Userid and BranchId=@BranchId ;";
                                string command7 = string.Format(QryUPT_Pd, "portaldb");

                                MySqlParameter[] parameters7 =
                               {
                                 new MySqlParameter("@Acess_Status",ObjCustPL.PageAccess),
                                 new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                 new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                 new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),
                                 new MySqlParameter("@Pid",ObjCustPL.Pid),
                                 new MySqlParameter("@Userid",Userid),
                                 new MySqlParameter("@BranchId",CmbBranchList.SelectedItem.Value),

                                };
                                K = ObjMain.ExecuteQuery(command7, parameters7);
                                
                            }
                            else
                            {
                                // Insert Query 
                                string QryPd = "Insert into  {0}.pagedetails (Pid, BranchId,Userid,Acess_Status,Read_Access,Write_Access,IDwld_Access )" +
                                    " Values(@Pid,@BranchId ,@Userid,@Acess_Status,@Read_Access ,@Write_Access ,@IDwld_Access );";
                                string command8 = string.Format(QryPd, "portaldb");

                                MySqlParameter[] parameters8 =
                                {
                                 new MySqlParameter("@Pid",ObjCustPL.Pid),
                                 new MySqlParameter("@BranchId",CmbBranchList.SelectedItem.Value),
                                 new MySqlParameter("@Userid",Userid),
                                 new MySqlParameter("@Acess_Status",ObjCustPL.PageAccess),
                                 new MySqlParameter("@Read_Access",ObjCustPL.ReadAccess),
                                 new MySqlParameter("@Write_Access",ObjCustPL.WriteAccess),
                                 new MySqlParameter("@IDwld_Access",ObjCustPL.IsDownload),
                                };



                                K = ObjMain.ExecuteQuery(command8,parameters8);

                            }
                        }




                    }
                }


                // Getting SubCompany's Details : 

                foreach (GridViewRow gvrow in grdChkMultiBranch.Rows)
                {
                    var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label BranchId_Val = gvrow.FindControl("lblBranchID") as Label;
                        Label branchname_Value = gvrow.FindControl("lblBranchName") as Label;
                        CheckBox ChkMulBr = gvrow.FindControl("chkBranchPageAccess") as CheckBox;

                        if (ChkMulBr.Checked == true)
                        {
                            ObjCustPL.IsMultiBranch = "Y";

                        }
                        else if (ChkMulBr.Checked == false)
                        {
                            ObjCustPL.IsMultiBranch = "N";
                        }



                        // Get cd_id in Companydetails 

                        string QueryA = "select cd_id from {0}.companydetails where  cm_id=@cm_id  and  cd_hq=1;";
                        string commandA = string.Format(QueryA, "portaldb");
                        MySqlParameter[] parametersA =
                        {

                         new MySqlParameter("@cm_id", hidCmid.Value),
                        };
                        string cd_id_Value = ObjMain.NewExecuteScalar(commandA, parametersA);



                        string QrUptSubcomp = "update  {0}.subcompmaster set  Multi_Access=@Multi_Access  where cm_id=@cm_id  and  cd_Branchid=@cd_Branchid and userid=@userid;";
                        string command9 = string.Format(QrUptSubcomp, "portaldb");

                        MySqlParameter[] parameters9 =
                        {
                         new MySqlParameter("@Multi_Access", ObjCustPL.IsMultiBranch),
                         new MySqlParameter("@cm_id", cd_id_Value),
                         new MySqlParameter("@cd_Branchid", BranchId_Val.Text),
                         new MySqlParameter("@userid", Userid),
                         
                        };


                        L = ObjMain.ExecuteQuery(command9,parameters9);
                        // Add the Insert code Here 

                    }
                }

               
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('User Rights has been Updated ');", true);
                

            }








        }

    }
}