简体   繁体   中英

Control based Security, cant delete from treeview the database entries,took code from controls-based-security-in-a-windows-forms-application

I want to delete from the treeview the nodes,and of course the roleid and userid.and on the other hand i also want to delete from listboxes rows,but i can delete them,but after restart application they are here again.it doesnt save

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using FirebirdSql.Data.FirebirdClient;

using System.Collections;

using System.Reflection;

namespace SiteYoenetim
{
public partial class ManageRoles : Form
{
private FbCommand cmd = null;
public ManageRoles()
{
InitializeComponent();
FillUsersInRollsTree();

}
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void AddNewRole_Click(object sender, EventArgs e)
{
string newName = string.Empty;
newName = NewRoleName.Text;
NewRoleName.Text = string.Empty; // clear the control
DataSet1.ROLESRow newRolesRow;
newRolesRow = DataSet1.ROLES.NewROLESRow();
newRolesRow.ROLENAME = newName;
this.DataSet1.ROLES.Rows.Add(newRolesRow);
try
{
this.rolesTableAdapter.Update(this.DataSet1.ROLES);
}
catch (Exception ex)
{
this.DataSet1.ROLES.Rows.Remove(newRolesRow);
MessageBox.Show("Unable to add role " + newName + ex.Message,
"Unable to add role!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
RolesListBox.SelectedIndex = -1;
        }
        private void DeleteRole_Click(object sender, EventArgs e)
        {
            string newName = string.Empty;
            newName = NewRoleName.Text;
            NewRoleName.Text = string.Empty; // clear the control
            DataSet1.ROLESRow newRolesRow;
            newRolesRow = DataSet1.ROLES.NewROLESRow();
            newRolesRow.ROLENAME = newName;
            this.DataSet1.ROLES.Rows.RemoveAt(RolesListBox.SelectedIndex);
            this.rolesTableAdapter.Update(this.DataSet1.ROLES);
        }
        private void AddNewAppUser_Click(object sender, EventArgs e)
        {
            DataSet1.USERSRow newUsersRow;
            newUsersRow = DataSet1.USERS.NewUSERSRow();
            newUsersRow.NAME = NewUserName.Text;
            NewUserName.Text = string.Empty;
            this.DataSet1.USERS.Rows.Add(newUsersRow);
            this.usersTableAdapter.Update(this.DataSet1.USERS);
            AppUsersListBox.SelectedIndex = -1;
        }

       HERE IT DELETES FROM APPUSER but it is again here after restart application

        private void DeleteAppUser_Click(object sender, EventArgs e)
        {
            DataSet1.USERSRow delUsersRow;
            delUsersRow = DataSet1.USERS.NewUSERSRow();
            delUsersRow.NAME = NewUserName.Text;
            NewUserName.Text = string.Empty;
            this.DataSet1.USERS.Rows.RemoveAt(AppUsersListBox.SelectedIndex);
            this.usersTableAdapter.Update(this.DataSet1.USERS);
        }
        private void AddUsersToRole_Click(object sender, EventArgs e)
        {
            ConnectionStringSettingsCollection connectionStrings =
                   ConfigurationManager.ConnectionStrings;
            string connString = connectionStrings["xxx.Properties.Settings.xxx"].ToString();
            FbConnection conn = new FbConnection(connString);
            conn.Open();
            FbParameter param;
            foreach (DataRowView userRow in AppUsersListBox.SelectedItems)
            {
                foreach (DataRowView roleRow in RolesListBox.SelectedItems)
                {
                    int userID = Convert.ToInt32(userRow["UserID"]);
                    int roleID = Convert.ToInt32(roleRow["RoleID"]);
                    try
                    {
                        cmd = new FbCommand("INSERT INTO usersToRoles (FKUserID, FKRoleID) values
 (@USERID, @RoleID)", conn);
                        param = cmd.Parameters.Add("@USERID", FbDbType.Integer);
                        param.Value = userID;
                        param.Direction = ParameterDirection.Input;
                        param = cmd.Parameters.Add("@RoleID", FbDbType.Integer);
                        param.Value = roleID;
                        param.Direction = ParameterDirection.Input;
                        int rowsInserted = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            conn.Close();
            FillUsersInRollsTree();

        }
        private void DisplayError(int userID, int roleID, string message)
        {
            MessageBox.Show("Unable to add user (" + userID + ") to role (" + roleID + ")" + message,
                "Unable to add user to role",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
        private void FillUsersInRollsTree()
        {
            ConnectionStringSettingsCollection connectionStrings =
       ConfigurationManager.ConnectionStrings;
            string connString = connectionStrings["xxx.Properties.Settings.xxx"].ToString();
            FbConnection conn = new FbConnection(connString);
            conn.Open();
            string queryString = "select u.Name, r.RoleName from userstoRoles utr " +
            " join users u on u.userID = utr.FKUserID " +
            " join Roles r on r.roleID = utr.FKRoleID ";
            if (rbName.Checked)
            {
                queryString += "order by Name";
            }
            else
            {
                queryString += "order by RoleName";
            }
            UsersInRoles.BeginUpdate();
            UsersInRoles.Nodes.Clear();
            TreeNode parentNode = null;
            TreeNode subNode = null;
            DataSet ds = new DataSet();
            FbDataAdapter dataAdapter = new FbDataAdapter(queryString, conn);
            dataAdapter.Fill(ds, "usersInRoles");
            DataTable dt = ds.Tables[0];
            string currentName = string.Empty;
            foreach (DataRow row in dt.Rows)
            {
                if (rbName.Checked)
                {
                    subNode = new TreeNode(row["roleName"].ToString());
                    if (currentName != row["Name"].ToString())
                    {
                        parentNode = new TreeNode(row["Name"].ToString());
                        currentName = row["Name"].ToString();
                        UsersInRoles.Nodes.Add(parentNode);
                    }
                }
                else
                {
                    subNode = new TreeNode(row["Name"].ToString());
                    if (currentName != row["RoleName"].ToString())
                    {
                        parentNode = new TreeNode(row["RoleName"].ToString());
                        currentName = row["RoleName"].ToString();
                        UsersInRoles.Nodes.Add(parentNode);
                    }
                }
                if (parentNode != null)
                {
                    parentNode.Nodes.Add(subNode);
                }
            }
            UsersInRoles.EndUpdate();
        }
        private void RadioButtonClick(object sender, EventArgs e)
        {
            FillUsersInRollsTree();
        }
        private void ManageRoles_Load(object sender, EventArgs e)
        {
                        this.uSERSTOROLESTableAdapter.Fill(this.dataSet11.USERSTOROLES);
                       this.uSERSTOROLESTableAdapter.Fill(this.DataSet1.USERSTOROLES);
                        this.usersTableAdapter.Fill(this.DataSet1.USERS);
                        this.rolesTableAdapter.Fill(this.DataSet1.ROLES);
        }
        private void Save_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.usersBindingSource.EndEdit();
            this.usersTableAdapter.Update(this.DataSet1);
        }

        HERE IT SHOULD DELETE FROM TREEVIEW, but it doesnt
        private void RemoveUsersFromRole_Click(object sender, EventArgs e)
        {
            ConnectionStringSettingsCollection connectionStrings =
                   ConfigurationManager.ConnectionStrings;
            string connString = connectionStrings["xxx.Properties.Settings.xxx"].ToString();
            FbConnection conn = new FbConnection(connString);
            conn.Open();
            FbParameter param;
            foreach (DataRowView userRow in AppUsersListBox.SelectedItems)
            {
                foreach (DataRowView roleRow in RolesListBox.SelectedItems)
                {
                    {
                        int userID = Convert.ToInt32(userRow["UserID"]);
                        int roleID = Convert.ToInt32(roleRow["RoleID"]);
                        try
                        {
                            cmd = new FbCommand("DELETE FROM usersToRoles (FKUserID, FKRoleID) values (USERID, RoleID)", conn);
                            param = cmd.Parameters.Add("USERID", FbDbType.Integer);
                            param.Value = userID;
                            param.Direction = ParameterDirection.Input;
                            param = cmd.Parameters.Add("RoleID", FbDbType.Integer);
                            param.Value = roleID;
                            param.Direction = ParameterDirection.Input;
                            int rowsInserted = cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                conn.Close();
                FillUsersInRollsTree();
            }
        }

    }
}

If your data is stored in a database, then deleting the data from the treeview and listboxes alone is not sufficient. You will have to delete the information in the database as well.


EDIT:

Your DELETE SQL command is wrong. Try to change it to:

DELETE FROM usersToRoles WHERE FKUserID = @USERID AND FKRoleID = @RoleID

(I was a mix between INSERT statement and DELETE statement.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM