Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i'm working on a practice project in which I have made two pages till now. one is login page and second in profile page. in profile page i have auto-populated user's info from database. I have made two buttons, one to Edit the info and other to Save Changes. The Problem is when i Click on SaveChanges button it never Updates the logged-in user's record. Here is the code of Edit and SaveChanges mathods:

C#
protected void btn_profileEdit_Click(object sender, EventArgs e)
        {
            tbox_profileDOB.Enabled = true;
            tbox_profileEmail.Enabled = true;
            tbox_profileFName.Enabled = true;
            tbox_profileGender.Enabled = true;
            tbox_profileGroupId.Enabled = true;
            tbox_profileLName.Enabled = true;
            tbox_profileUserName.Enabled = true;

        }

        protected void btn_profileSaveChanges_Click(object sender, EventArgs e)
        {  
            if (Session["userName"] != null)
            {
                string userName = Session["userName"].ToString();
                var userDetails = (from user in db.web_users
                                   join stats in db.user_stats on user.Id equals stats.UserId
                                   where user.UserName == userName
                                   select new { user, stats }).FirstOrDefault();

                //db.web_users.Remove(userDetails.user);
                //db.user_stats.Remove(userDetails.stats);
                
                userDetails.user.DOB = Convert.ToDateTime(tbox_profileDOB.Text);
                userDetails.user.Email = tbox_profileEmail.Text;
                userDetails.user.FirstName = tbox_profileFName.Text;
                userDetails.user.Gender = tbox_profileGender.Text;
                userDetails.user.LastName = tbox_profileLName.Text;
                userDetails.user.UserName = tbox_profileUserName.Text;
                userDetails.stats.GroupId = Convert.ToInt32(tbox_profileGroupId.Text);

                //db.web_users.Add(userDetails.user);
                //db.user_stats.Add(userDetails.stats);
                db.SaveChanges();
                
                tbox_profileDOB.Enabled = false;
                tbox_profileEmail.Enabled = false;
                tbox_profileFName.Enabled = false;
                tbox_profileGender.Enabled = false;
                tbox_profileGroupId.Enabled = false;
                tbox_profileLName.Enabled = false;
                tbox_profileUserName.Enabled = false;
             }
             else
             {
                 Response.Redirect("Login.aspx");
             }
        }
Posted
Updated 5-Jan-15 4:23am
v2
Comments
Taucher Christoph 5-Jan-15 10:35am    
Do you get an error, if not check the SaveChange() function.
Debug the btn Event?
Hamza Javed 5-Jan-15 10:56am    
I'm not getting any error. when this page(Profile.aspx) is loaded, all the user's info is non-editable. Edit button make them editable. let i changed the UserName and email and Click on SaveChanges button. In debug mode i saw that text boxes of this page in "btn_profileSaveChanges_Click" never show/have the data which i write in editing mode in these textBoxes. these textBoxes always show the data which is auto-populated on pageLoad.
second thing is the "db.SaveChanges()" is built-in function. it is just used to save any change in database like "SubmitChanges()".
Anisuzzaman Sumon 5-Jan-15 23:19pm    
Use ApplyChanges( userDetails) before db.SaveChanges() to update
Hamza Javed 7-Jan-15 11:02am    
Can you please elaborate because there is no any property showing of ApplyChanges in my case. i tried following:
db.web_user.ApplyChanges(UserDetails);
and
db.ApplyChanges(UserDetails);
both are not working :(
deepankarbhatnagar 5-Jan-15 23:36pm    
Check if your query gives correct output or not

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900