public List<UserRollModel> GetAllAccessSpecificRollsByAccessValue(int userAccessNameId)
 {
     List<UserRollModel> userRollModels = new List<UserRollModel>();
     string query = String.Format("Select UserRollValue,RollStatus from tblUserRoll where UserAccessNameId='{0}' order by UserRollValue ", userAccessNameId);
     sqlCommand.CommandText = query;
     using (sqlConnection)
     {
         sqlConnection.Open();
         SqlDataReader rdr = sqlCommand.ExecuteReader();
         while (rdr.Read())
         {
             UserRollModel userRollModel = new UserRollModel();
             userRollModel.RollValue = rdr[0].ToString();
             userRollModel.Status = rdr[1].ToString();
             userRollModels.Add(userRollModel);
         }
         sqlConnection.Close();
     }
     return userRollModels;
 }
 public int UpdateRoolsOfSelectedUserAccess(UserRollModel userRollModel)
 {
     int rowsUpdated = 0;
     string query = String.Format("Update tblUserRoll set RollStatus='{0}' where UserAccessNameId='{1}'", userRollModel.Status,userRollModel.AccessRollId);
     using (SqlConnection con7 = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString) )
     {
         SqlCommand cmd7 = new SqlCommand(query,con7);
         con7.Open();
         rowsUpdated = cmd7.ExecuteNonQuery();
         con7.Close();
     }
     return rowsUpdated;
 }
        protected void createButton_Click(object sender, EventArgs e)
        {
            int lastIdentityOfUserAccess = 0;
            UserAccessModel userAccessModel = new UserAccessModel();
            userAccessModel.UserAccessName = userAccessTextBox.Text;
            if (userAccessTextBox.Text=="")
            {
                statusLabel.Text = "Please enter the name";
            }
            else
            {
                if (IsUserAccessNameExist(userAccessTextBox.Text))
                {
                    statusLabel.Text = "This user access name already exist. Try with another name.";
                }
                else
                {
                    if (SaveUserAccessName(userAccessModel) > 0)
                    {
                        lastIdentityOfUserAccess = GetLastIdentityOfUserAccess();

                        foreach (ListItem item in perDealershipOverridesCheckBoxList.Items)
                        {
                            UserRollModel userRollModel = new UserRollModel();
                            userRollModel.RollValue = item.Value;
                            if (item.Selected)
                            {
                                userRollModel.Status = "True";
                            }
                            else
                            {
                                userRollModel.Status = "False";
                            }
                            userRollModel.AccessRollId = lastIdentityOfUserAccess;
                            if (SaveUserRolls(userRollModel) > 0)
                            {
                                statusLabel.ForeColor = System.Drawing.Color.Green;
                                statusLabel.Text = "User rolls saved.";
                                GetAllUserAccessNameInDropdownList();
                                ListItem li = new ListItem("Select user access", "-1");
                                userAccessDropDownlist.Items.Insert(0, li);
                            }
                            else
                            {
                                statusLabel.Text = "User rolls not saved.";
                                GetAllUserAccessNameInDropdownList();
                                ListItem li = new ListItem("Select user access", "-1");
                                userAccessDropDownlist.Items.Insert(0, li);
                            }
                        }
                        foreach (ListItem item in globalRulesCheckBoxList.Items)
                        {
                            UserRollModel userRollModel = new UserRollModel();
                            userRollModel.RollValue = item.Value;
                            if (item.Selected)
                            {
                                userRollModel.Status = "True";
                            }
                            else
                            {
                                userRollModel.Status = "False";
                            }
                            userRollModel.AccessRollId = lastIdentityOfUserAccess;
                            if (SaveUserRolls(userRollModel) > 0)
                            {
                                statusLabel.ForeColor = System.Drawing.Color.Green;
                                statusLabel.Text = "User rolls saved.";
                                GetAllUserAccessNameInDropdownList();
                                ListItem li = new ListItem("Select user access", "-1");
                                userAccessDropDownlist.Items.Insert(0, li);

                            }
                            else
                            {
                                statusLabel.Text = "User rolls not saved.";
                                GetAllUserAccessNameInDropdownList();
                                ListItem li = new ListItem("Select user access", "-1");
                                userAccessDropDownlist.Items.Insert(0, li);
                            }

                        }
                        DeSelectItemInCheckBoxList();
                    }
                    else
                    {
                        statusLabel.Text = "User Access not saved.";
                    }
                }
            }
        }
        public int SaveUserRolls(UserRollModel userRollModel)
        {
            int rowsInserted = 0;

            string query = String.Format("Insert into tblUserRoll values('{0}','{1}','{2}')",userRollModel.RollValue,userRollModel.Status,userRollModel.AccessRollId);
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(query,con);
                con.Open();
                rowsInserted = cmd.ExecuteNonQuery();
                con.Close();

            }
            return rowsInserted;
        }