简体   繁体   English

使用SQL的C#编程问题?

[英]C# programming issue using sql?

My problem is not my stored procedure but the fact that somewhere in my code it wont allow me to change the role in the database. 我的问题不是我的存储过程,而是我的代码中的某个地方不允许我更改数据库中的角色。 Although i know it is written correctly could someone please have a good look at my code as iam really frustrated atm. 尽管我知道它的编写正确,但是有人会因为我对atm感到沮丧而对我的代码有所了解。 Thankyou in advanced.. 谢谢。

DAO - 道-

    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();
        }

Manager - 经理 -

       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;
    }

The page - 这一页 -

       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;                
        }                      

    }

The problem is you are using selected value of dropdown as username 问题是您使用下拉列表的选定值作为用户名

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

where as when you are binding it with datasource, The value field is AccountId 当您将其与数据源绑定时,值字段为AccountId

ddlUser.DataValueField = "AccountId";

So in your function AccountId is actually passing as UserName of that user. 因此,在您的函数中, AccountId实际上是作为该用户的UserName传递的。 So that makes your query with unusual result. 这样会使您的查询结果异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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