2

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

    }
2
  • Did you try to debug the stored procedure with the inputs in real time? That might give you better information. Also try to get any error message from the database. Commented May 17, 2013 at 4:07
  • No i did not in the database i am using int. so user is 0 and admin is 1. the problem is no exception and i can not figure out why. I did originally have in the dao cmd.Parameters.Add(new SqlParameter("@role", account.Role); <edited this to role that should of worked but no luck. Commented May 17, 2013 at 4:08

1 Answer 1

1

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

ddlUser.DataValueField = "AccountId";

So in your function AccountId is actually passing as UserName of that user. So that makes your query with unusual result.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou so much been puzzled over that for so long..... Feel blonde ddlUser.DataValueField = "AccountId"; shud be "Username" thankyou again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.