0

Alright, so I want an option for admin to change someone's username in gridview like I change his email, comment etc.. These are my codes:

protected void UserAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
    UserAccounts.EditIndex = e.NewEditIndex;
    BindUserAccounts();
}
protected void UserAccounts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int index = UserAccounts.EditIndex;
    GridViewRow row = UserAccounts.Rows[e.RowIndex];
    username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
    email = ((TextBox)row.Cells[2].Controls[0]).Text;
    approvedchange = ((CheckBox)row.Cells[3].Controls[0]).Checked;
    comment = ((TextBox)row.Cells[6].Controls[0]).Text;
    MembershipUser user = Membership.GetUser(username);
    if (user != null)
    {
        bool edited = false;
        ActionStatus.Text = string.Format("<font size=4><b><u>Editing {0}</u></b></font>", username);
        if (!user.Email.Equals(email))
        {
            edited = true;
            user.Email = email;
            ActionStatus.Text += "<br />Email has been successfully updated!";
        }
        if (!user.IsApproved == approvedchange)
        {
            edited = true;
            user.IsApproved = approvedchange;
            ActionStatus.Text += "<br />Approved status has been successfully updated!";
        }
        if (!user.Comment.Equals(comment))
        {
            edited = true;
            user.Comment = comment;
            ActionStatus.Text += "<br />Comment has been successfully updated!";
        }
        if(edited)
            Membership.UpdateUser(user);
        else
            ActionStatus.Text += string.Format("<br />You haven't edited any of {0}'s details, so they haven't been updated!", username);
    }
    else
        ActionStatus.Text += string.Format("ERROR: user == null");
    UserAccounts.EditIndex = -1;
    BindUserAccounts();
}

Well, how do I do it? Thanks for those who help!

Aradmey
  • 377
  • 1
  • 3
  • 11

1 Answers1

0

MembershipProvider doesn't allow changing Username.

Membership.UpdateUser(user); updates the following information only -

  • Email
  • Comment
  • IsApproved
  • LastLoginDate

If you want to change it, you need to create a function outside of Membership Provider.

Win
  • 61,100
  • 13
  • 102
  • 181
  • I know, that's why I'm asking here. Do you know how can I do it? – Aradmey Oct 17 '14 at 21:42
  • It all depends on your Data Access layer such as Entity Framework, NHibernate, ADO.Net and so. Look at [here](http://stackoverflow.com/questions/2141952/manually-changing-username-in-asp-net-membership?rq=1) and [here](http://stackoverflow.com/questions/1001491/is-it-possible-to-change-the-username-with-the-membership-api?rq=1) – Win Oct 17 '14 at 21:57
  • How could I know which do I use? – Aradmey Oct 19 '14 at 01:43