2

我的问题不是我的存储过程,而是我的代码中某处不允许我更改数据库中的角色的事实。虽然我知道它写得正确,但有人可以好好看看我的代码,因为我真的很沮丧 atm。谢谢你先进..

道 -

    public void EditRole(Account account, RoleEnum role)
    {
        using (SqlConnection connection = ConnectionDao.GetConnection())
        {
            SqlCommand cmd = new SqlCommand("sp_Accounts_EditRoleByUsername", connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;    

            cmd.Parameters.Add(new SqlParameter("@role", role);
            cmd.Parameters.Add(new SqlParameter("@username", account.Username));              

            cmd.ExecuteNonQuery();
        }

经理 -

       public static ResultEnum RoleChange(Account account, RoleEnum role)
    {
        ResultEnum result = ResultEnum.Success;

        try
        {
            AccountDao dao = new AccountDao();
            dao.EditRole(account, role);
        }
        catch (Exception)
        {
            result = ResultEnum.Error;
        }
        return result;
    }

这页纸 -

       public partial class ManageRolesPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Result<List<Account>> result = AccountManager.GetAll();
            if (result.ResultEnum == ResultEnum.Success)
            {
                ddlUser.DataSource = result.Data;
                ddlUser.DataTextField = "Username";
                ddlUser.DataValueField = "AccountId";
                ddlUser.DataBind();
            }
            else
            {
                lblInfo.Text = "database error";
            }
        }

    }

    protected void btnPermission_Click(object sender, EventArgs e)
    {

        Account account = new Account
        {
            Username = ddlUser.SelectedValue
        };

        RoleEnum role;

        if (rdlRole.SelectedValue == "Admin")
        {
            role = RoleEnum.Admin;

        }
        else
        {                
            role = RoleEnum.User;
        }



         ResultEnum result = AccountManager.RoleChange(account, role);

        switch (result)
        {
            case ResultEnum.Success:
                lblInfo.Text = "User: " + ddlUser.SelectedItem + " Has been edited to " + role;
                break;
            case ResultEnum.Error:
                lblInfo.Text = "Error";
                break;                
        }                      

    }
4

1 回答 1

1

问题是您使用下拉列表的选定值作为用户名

Account account = new Account
        {
            Username = ddlUser.SelectedValue
        };

当你将它与数据源绑定时,值字段是AccountId

ddlUser.DataValueField = "AccountId";

所以在你的函数AccountId中实际上是从UserName那个用户传递过来的。因此,这会使您的查询产生不寻常的结果。

于 2013-05-17T04:08:59.960 回答