public static void SaveRoleModule(SecRoleModule secRoleModule)
 {
     DbContextHelper.DbContext.Add(secRoleModule);
     DbContextHelper.SaveChanges();
 }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblError.Text    = string.Empty;
            lblSave.Text     = string.Empty;
            lblSave.Visible  = false;
            lblError.Visible = false;
            try {
                using (TransactionScope scope = new TransactionScope()) {
                    SecRoleModuleManager.UpdateSecRoleModuleStatus(Convert.ToInt32(ddlRole.SelectedValue));

                    foreach (GridViewRow row in grdModule.Rows)
                    {
                        int         check          = 0;
                        int         checkParent    = 0;
                        GridView    gv             = (GridView)row.FindControl("gvSubModule");
                        HiddenField hfRoleModuleId = (HiddenField)row.FindControl("hfRoleModuleId");
                        HiddenField hfModuleId     = (HiddenField)row.FindControl("hfModuleId");

                        if (hfRoleModuleId.Value == string.Empty)
                        {
                            hfRoleModuleId.Value = "0";
                        }

                        CheckBox chkAllAdd  = (CheckBox)row.FindControl("chkAllAdd");
                        CheckBox chkAllEdit = (CheckBox)row.FindControl("chkAllEdit");
                        CheckBox chkAllDel  = (CheckBox)row.FindControl("chkAllDel");
                        CheckBox chkAllView = (CheckBox)row.FindControl("chkAllView");

                        SecRoleModule roleModuleMainGrid = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                        if (chkAllView.Checked || chkAllAdd.Checked || chkAllEdit.Checked || chkAllEdit.Checked)
                        {
                            var secRoleModule = new CRM.Data.Entities.SecRoleModule {
                                ModuleID         = Convert.ToInt32(hfModuleId.Value),
                                RoleID           = Convert.ToInt32(ddlRole.SelectedValue),
                                AddPermssion     = chkAllAdd.Checked == true ? true : false,
                                EditPermission   = chkAllEdit.Checked == true ? true : false,
                                DeletePermission = chkAllDel.Checked == true ? true : false,
                                ViewPermission   = chkAllView.Checked == true ? true : false,
                                Status           = 1,
                                CreatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                CreatedOn        = DateTime.Now,
                                UpdatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                UpdatedOn        = DateTime.Now,
                                CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),
                            };
                            SecRoleModuleManager.SaveRoleModule(secRoleModule);
                            checkParent = 1;
                        }


                        foreach (GridViewRow subGridRow in gv.Rows)
                        {
                            CheckBox      chkAdd          = (CheckBox)subGridRow.FindControl("chkAdd");
                            CheckBox      chkEdit         = (CheckBox)subGridRow.FindControl("chkEdit");
                            CheckBox      chkDelete       = (CheckBox)subGridRow.FindControl("chkDel");
                            CheckBox      chkView         = (CheckBox)subGridRow.FindControl("chkView");
                            HiddenField   hfModuleIdChild = (HiddenField)subGridRow.FindControl("hfModuleIdChild");
                            SecRoleModule roleModule      = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                            if (chkAdd.Checked || chkEdit.Checked || chkDelete.Checked || chkView.Checked)
                            {
                                var secRoleModuleChild = new CRM.Data.Entities.SecRoleModule {
                                    ModuleID         = Convert.ToInt32(hfModuleIdChild.Value),
                                    RoleID           = Convert.ToInt32(ddlRole.SelectedValue),
                                    AddPermssion     = chkAdd.Checked == true ? true : false,
                                    EditPermission   = chkEdit.Checked == true ? true : false,
                                    DeletePermission = chkDelete.Checked == true ? true : false,
                                    ViewPermission   = chkView.Checked == true ? true : false,
                                    Status           = 1,
                                    CreatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                    CreatedOn        = DateTime.Now,
                                    UpdatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                    UpdatedOn        = DateTime.Now,
                                    CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),
                                };
                                SecRoleModuleManager.SaveRoleModule(secRoleModuleChild);
                                check       = 1;
                                checkParent = 0;
                            }

                            GridView gvchild = (GridView)subGridRow.FindControl("gvSubModule1");
                            if (gvchild.Rows.Count > 0)
                            {
                                foreach (GridViewRow child in gvchild.Rows)
                                {
                                    CheckBox      chkAdd1          = (CheckBox)child.FindControl("chkAdd");
                                    CheckBox      chkEdit1         = (CheckBox)child.FindControl("chkEdit");
                                    CheckBox      chkDelete1       = (CheckBox)child.FindControl("chkDel");
                                    CheckBox      chkView1         = (CheckBox)child.FindControl("chkView");
                                    HiddenField   hfModuleIdChild1 = (HiddenField)child.FindControl("hfModuleIdChild1");
                                    SecRoleModule roleModule1      = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                                    if (chkAdd1.Checked || chkEdit1.Checked || chkDelete1.Checked || chkView1.Checked)
                                    {
                                        var secRoleModuleChild = new CRM.Data.Entities.SecRoleModule {
                                            ModuleID         = Convert.ToInt32(hfModuleIdChild1.Value),
                                            RoleID           = Convert.ToInt32(ddlRole.SelectedValue),
                                            AddPermssion     = chkAdd.Checked == true ? true : false,
                                            EditPermission   = chkEdit.Checked == true ? true : false,
                                            DeletePermission = chkDelete.Checked == true ? true : false,
                                            ViewPermission   = chkView.Checked == true ? true : false,
                                            Status           = 1,
                                            CreatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                            CreatedOn        = DateTime.Now,
                                            UpdatedBy        = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                            UpdatedOn        = DateTime.Now,
                                            CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),
                                        };
                                        SecRoleModuleManager.SaveRoleModule(secRoleModuleChild);
                                        check       = 1;
                                        checkParent = 0;
                                    }
                                }
                            }
                        }
                    }

                    CRM.Data.Entities.SecUser user = SecUserManager.GetByUserId(Convert.ToInt32(HttpContext.Current.User.Identity.Name));
                    if (user.SecRole.RoleId == Convert.ToInt32(ddlRole.SelectedValue))
                    {
                        Session["rolePermission"] = refreshRolePermission = SecRoleModuleManager.getRolePermission(user.SecRole.RoleId).ToList();
                    }
                    bindGrid(Convert.ToInt32(ddlRole.SelectedValue));
                    lblSave.Text    = "Record Saved Sucessfully.";
                    lblSave.Visible = true;
                    scope.Complete();
                }
            }
            catch (Exception ex) {
                lblError.Text    = "Record Not Saved Sucessfully.";
                lblError.Visible = true;
            }
        }
        public static void Save(SecRoleModule secRoleModule)
        {
            if (secRoleModule.RoleModuleId == 0) {
                //secRoleModule.CreatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name);
                secRoleModule.CreatedBy = 1;
                secRoleModule.CreatedOn = DateTime.Now;
                DbContextHelper.DbContext.Add(secRoleModule);
            }

            //secRoleModule.UpdatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name);
            secRoleModule.UpdatedBy = 1;
            secRoleModule.UpdatedOn = DateTime.Now;
            DbContextHelper.DbContext.SaveChanges();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            lblSave.Text = string.Empty;
            lblSave.Visible = false;
            lblError.Visible = false;
            try {
                using (TransactionScope scope = new TransactionScope()) {
                    SecRoleModuleManager.UpdateSecRoleModuleStatus(Convert.ToInt32(ddlRole.SelectedValue));

                    foreach (GridViewRow row in grdModule.Rows) {
                        int check = 0;
                        int checkParent = 0;
                        GridView gv = (GridView)row.FindControl("gvSubModule");
                        HiddenField hfRoleModuleId = (HiddenField)row.FindControl("hfRoleModuleId");
                        HiddenField hfModuleId = (HiddenField)row.FindControl("hfModuleId");

                        if (hfRoleModuleId.Value == string.Empty)
                            hfRoleModuleId.Value = "0";

                        CheckBox chkAllAdd = (CheckBox)row.FindControl("chkAllAdd");
                        CheckBox chkAllEdit = (CheckBox)row.FindControl("chkAllEdit");
                        CheckBox chkAllDel = (CheckBox)row.FindControl("chkAllDel");
                        CheckBox chkAllView = (CheckBox)row.FindControl("chkAllView");

                        SecRoleModule roleModuleMainGrid = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                        if (chkAllView.Checked || chkAllAdd.Checked || chkAllEdit.Checked || chkAllEdit.Checked) {
                            var secRoleModule = new CRM.Data.Entities.SecRoleModule {
                                ModuleID = Convert.ToInt32(hfModuleId.Value),
                                RoleID = Convert.ToInt32(ddlRole.SelectedValue),
                                AddPermssion = chkAllAdd.Checked == true ? true : false,
                                EditPermission = chkAllEdit.Checked == true ? true : false,
                                DeletePermission = chkAllDel.Checked == true ? true : false,
                                ViewPermission = chkAllView.Checked == true ? true : false,
                                Status = 1,
                                CreatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                CreatedOn = DateTime.Now,
                                UpdatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                UpdatedOn = DateTime.Now,
                                CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),
                            };
                            SecRoleModuleManager.SaveRoleModule(secRoleModule);
                            checkParent = 1;
                        }

                        foreach (GridViewRow subGridRow in gv.Rows) {
                            CheckBox chkAdd = (CheckBox)subGridRow.FindControl("chkAdd");
                            CheckBox chkEdit = (CheckBox)subGridRow.FindControl("chkEdit");
                            CheckBox chkDelete = (CheckBox)subGridRow.FindControl("chkDel");
                            CheckBox chkView = (CheckBox)subGridRow.FindControl("chkView");
                            HiddenField hfModuleIdChild = (HiddenField)subGridRow.FindControl("hfModuleIdChild");
                            SecRoleModule roleModule = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                            if (chkAdd.Checked || chkEdit.Checked || chkDelete.Checked || chkView.Checked) {
                                var secRoleModuleChild = new CRM.Data.Entities.SecRoleModule {
                                    ModuleID = Convert.ToInt32(hfModuleIdChild.Value),
                                    RoleID = Convert.ToInt32(ddlRole.SelectedValue),
                                    AddPermssion = chkAdd.Checked == true ? true : false,
                                    EditPermission = chkEdit.Checked == true ? true : false,
                                    DeletePermission = chkDelete.Checked == true ? true : false,
                                    ViewPermission = chkView.Checked == true ? true : false,
                                    Status = 1,
                                    CreatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                    CreatedOn = DateTime.Now,
                                    UpdatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                    UpdatedOn = DateTime.Now,
                                    CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),

                                };
                                SecRoleModuleManager.SaveRoleModule(secRoleModuleChild);
                                check = 1;
                                checkParent = 0;
                            }

                            GridView gvchild = (GridView)subGridRow.FindControl("gvSubModule1");
                            if (gvchild.Rows.Count > 0) {
                                foreach (GridViewRow child in gvchild.Rows) {
                                    CheckBox chkAdd1 = (CheckBox)child.FindControl("chkAdd");
                                    CheckBox chkEdit1 = (CheckBox)child.FindControl("chkEdit");
                                    CheckBox chkDelete1 = (CheckBox)child.FindControl("chkDel");
                                    CheckBox chkView1 = (CheckBox)child.FindControl("chkView");
                                    HiddenField hfModuleIdChild1 = (HiddenField)child.FindControl("hfModuleIdChild1");
                                    SecRoleModule roleModule1 = SecRoleModuleManager.GetByRoleModuleId(Convert.ToInt32(hfRoleModuleId.Value));
                                    if (chkAdd1.Checked || chkEdit1.Checked || chkDelete1.Checked || chkView1.Checked) {
                                        var secRoleModuleChild = new CRM.Data.Entities.SecRoleModule {
                                            ModuleID = Convert.ToInt32(hfModuleIdChild1.Value),
                                            RoleID = Convert.ToInt32(ddlRole.SelectedValue),
                                            AddPermssion = chkAdd.Checked == true ? true : false,
                                            EditPermission = chkEdit.Checked == true ? true : false,
                                            DeletePermission = chkDelete.Checked == true ? true : false,
                                            ViewPermission = chkView.Checked == true ? true : false,
                                            Status = 1,
                                            CreatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                            CreatedOn = DateTime.Now,
                                            UpdatedBy = Convert.ToInt32(HttpContext.Current.User.Identity.Name),
                                            UpdatedOn = DateTime.Now,
                                            CreatedMachineIP = Request.ServerVariables["remote_addr"].ToString(),

                                        };
                                        SecRoleModuleManager.SaveRoleModule(secRoleModuleChild);
                                        check = 1;
                                        checkParent = 0;
                                    }
                                }
                            }

                        }

                    }

                    CRM.Data.Entities.SecUser user = SecUserManager.GetByUserId(Convert.ToInt32(HttpContext.Current.User.Identity.Name));
                    if (user.SecRole.RoleId == Convert.ToInt32(ddlRole.SelectedValue)) {
                        Session["rolePermission"] = refreshRolePermission = SecRoleModuleManager.getRolePermission(user.SecRole.RoleId).ToList();
                    }
                    bindGrid(Convert.ToInt32(ddlRole.SelectedValue));
                    lblSave.Text = "Record Saved Sucessfully.";
                    lblSave.Visible = true;
                    scope.Complete();
                }
            }
            catch (Exception ex) {
                lblError.Text = "Record Not Saved Sucessfully.";
                lblError.Visible = true;
            }
        }