Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:48:43+00:00 2026-05-14T01:48:43+00:00

I have a section on a page where a user can change their username.

  • 0

I have a section on a page where a user can change their username. This is basically the code for that:

user.UserName = txtNewUserName.Text;
user.Save();

user is a class in my dbml and Save calls SubmitChanges on the data context.

The name is changed in the database, so when it calls the following method, it fails because it is trying to get the user with the old username instead of the one that is in the database now.

public override string[] GetRolesForUser(string username)
{
    return dc.Users.Where(u => u.UserName== username)
                   .SingleOrDefault().Roles
                   .Select(r => r.RoleName).ToArray<string>();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:48:44+00:00Added an answer on May 14, 2026 at 1:48 am

    It looks as you keep the dc used in GetRolesForUser around for some time. So if you have retrived the user before and then request the same user again, using the same data context it will return the user cached by that data context.

    Try using

    public override string[] GetRolesForUser(string username)
    {
        using (var ctx = new MyDataContext()) {
            return ctx.Users.Where(u => u.UserName== username)
                            .SingleOrDefault().Roles
                            .Select(r => r.RoleName).ToArray<string>();
        }
    }
    

    Edit

    Well if the username parameter holds the old user name, the function you provided works correctly in not returning any roles for the old user … make sure that you pass the new username into the function. I.e. it is a problem of where you hold the value that gets passed into the funcion, and not a problem of the function.

    2nd Edit (After knowing that a RoleProvider is used)

    Saving the user to the database does not alter the credentials of the current user. These are set during login. To verify cou can check whether HttpContext.User still holds the user name. So what you need to do is to either force the uer to log in again, or use an imutable indentifier as the credential, e.g. a guid assigned to each user, then the user name can change and the roles still can be resolved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a user account page where users can change their settings, and the
I have a div like this <section class=item_container> <article class=item> <h2>Page Title</h2> <p><a class=delete_page
I have a section on my website that when a user clicks I would
I have a section of my site where users can add addresses to their
I have a page on my MVC application where a user can edit /
I have a text box on a HTML page, the idea is that the
I am creating a web page and have a section that I want to
I have a section in my ASP.NET MVC3 website where a user can click
On my page I have a section where I would like to pull in
On a blog detail page, I have a tags section. The html looks like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.