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 7790699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:39:53+00:00 2026-06-01T21:39:53+00:00

I am creating a web app, and within this app I want to allow

  • 0

I am creating a web app, and within this app I want to allow users to update their user information (first name, last name, email, phone number, company name) or change their password. When the page loads, their current information is entered into the text boxes. My problem is that I don’t know how to apply the changes the user makes. My code currently looks like this:

    DataClasses1DataContext context = new DataClasses1DataContext();
    User user = new User();


    protected void Page_Load(object sender, EventArgs e)
    {
        string usr = Session["SessionUserName"].ToString();

        user = (from u in context.Users
                    where u.username.Equals(usr)
                    select u).FirstOrDefault();

        txtFName.Text = user.firstName;
        txtLName.Text = user.lastName;
        txtCompany.Text = user.companyName;
        txtEmail.Text = user.email;
        txtPhone.Text = user.phoneNumber;


    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        using (DataClasses1DataContext context2 = new DataClasses1DataContext())
        {
            User nUser = (from u in context2.Users
                          where u.username == user.username
                          select u).SingleOrDefault();

            if (nUser != null)
            {
                //make the changes to the user
                nUser.firstName = txtFName.Text;
                nUser.lastName = txtLName.Text;
                nUser.email = txtEmail.Text;
                if (!String.IsNullOrEmpty(txtPhone.Text))
                    nUser.phoneNumber = txtPhone.Text;
                if (!String.IsNullOrEmpty(txtCompany.Text))
                    nUser.companyName = txtCompany.Text;
                nUser.timeStamp = DateTime.Now;
            }


            //submit the changes
            context2.SubmitChanges();

            Response.Redirect("Projects.aspx");

        }

This doesn’t give me any errors, but it also doesn’t apply the changes. Some google searching led me to add

context2.Users.Attach(nUser);

but that gave me the error message: System.InvalidOperationException: Cannot attach an entity that already exists. So I tried

context2.Users.Attach(nUser, true); 

and I got the same error message.

Looking at google some more I found information about detaching an entity first, but that information was about 3 years old and doesn’t appear to be valid anymore (as context.Detach or context.Users.Detach are not options and give me build errors if I try to use them). Other error message I received (that I don’t remember how I got to honestly) suggested that I change the update policy so I set all fields under the Users class to never for the update policy. There was suggestions to deserialize and then serialize the data again, but I couldn’t find any information on how to do that. Any help would be appreciated.

EDIT: Changed the code due to some suggestions below, but it’s still not working. I do not get any error messages, but the data is not being saved.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string usr = Session["SessionUserName"].ToString();

            user = (from u in context.Users
                    where u.username.Equals(usr)
                    select u).FirstOrDefault();

            txtFName.Text = user.firstName;
            txtLName.Text = user.lastName;
            txtCompany.Text = user.companyName;
            txtEmail.Text = user.email;
            txtPhone.Text = user.phoneNumber;
        }  

    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {



                //make the changes to the user
                user.firstName = txtFName.Text;
                user.lastName = txtLName.Text;
                user.email = txtEmail.Text;
                if (!String.IsNullOrEmpty(txtPhone.Text))
                    user.phoneNumber = txtPhone.Text;
                if (!String.IsNullOrEmpty(txtCompany.Text))
                    user.companyName = txtCompany.Text;
                user.timeStamp = DateTime.Now;
                //submit the changes
            context.SubmitChanges();

            Response.Redirect("Projects.aspx");

      }
  • 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-06-01T21:39:54+00:00Added an answer on June 1, 2026 at 9:39 pm

    This is happening because your writing the same info back to those textboxes before the update. All you need to do is change your page_load to this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string usr = Session["SessionUserName"].ToString();
    
            user = (from u in context.Users
                    where u.username.Equals(usr)
                    select u).FirstOrDefault();
    
            txtFName.Text = user.firstName;
            txtLName.Text = user.lastName;
            txtCompany.Text = user.companyName;
            txtEmail.Text = user.email;
            txtPhone.Text = user.phoneNumber;
        }
    }
    

    The page_load is executing before your update. Therefore, it is reassigning the original values and then updating them with the same. Hope this helps! Good Luck!

    EDIT: Changes made after question was updated:

    Change your entire code to this. I have also added some need checking that you should be handling and removed your global variables from the top:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["SessionUserName"] != null)
            {
                string usr = Session["SessionUserName"].ToString();
    
                DataClasses1DataContext context = new DataClasses1DataContext();
                User user = (from u in context.Users
                             where u.username.Equals(usr)
                             select u).FirstOrDefault();
    
                if (user != null)
                {
                    txtFName.Text = user.firstName;
                    txtLName.Text = user.lastName;
                    txtCompany.Text = user.companyName;
                    txtEmail.Text = user.email;
                    txtPhone.Text = user.phoneNumber;
                }
                else
                {
                    //--- handle user not found error
                }
            }
            else
            {
                //--- handle session is null
            }
        }
    }
    
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Session["SessionUserName"] != null)
        {
            string usr = Session["SessionUserName"].ToString();
    
            try
            {
                DataClasses1DataContext context = new DataClasses1DataContext();
                User nUser = (from u in context.Users
                              where u.username.Equals(usr)
                              select u).FirstOrDefault();
    
                //make the changes to the user
                nUser.firstName = txtFName.Text;
                nUser.lastName = txtLName.Text;
                nUser.email = txtEmail.Text;
                if (!String.IsNullOrEmpty(txtPhone.Text))
                    nUser.phoneNumber = txtPhone.Text;
                if (!String.IsNullOrEmpty(txtCompany.Text))
                    nUser.companyName = txtCompany.Text;
                nUser.timeStamp = DateTime.Now;
    
                //submit the changes
                context.SubmitChanges();
    
                Response.Redirect("Projects.aspx");
            }
            catch (Exception ex)
            {
                //--- handle update error
            }
        }
        else
        {
            //--- handle null session error
        }
    }
    

    That should work out for you. Let me know! Good Luck!

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

Sidebar

Related Questions

I'm creating a web app that allows users to enter a number of colors,
I'm creating a web app with various classes for things like the user, Smarty
I'm creating quick web app that needs to send a php-created message from within
I'm creating a web app where users can specify a time and date to
Im creating a web app where I want all of the responses to the
i'm creating a web app that's running on an Advantage Database server, not my
I'm creating a web app with Django. Since I'm very familiar with Apache I
I'm creating a web app (locally, so security doesn't matter) in PHP where the
I am creating a web app which uses jQuery to authenticate: $.ajax({ url: /session/create?format=json,
We are creating a web app where we need to have concurrency for a

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.