locked
Updating the existing row in a table with session data RRS feed

  • Question

  • User351619809 posted

    Hello All,

    I have an entire class object stored in my session. I want to update an existing row in a table with the session data. I am trying to find out if their any better way. In below code, I am trying to get  the data from Session["MailingInfo"] and trying to update the data in the existing table. I tried to do this:

    IDData = MI

    That didn't work so I tried this way and it worked. I was wondering if their is any simple way to achieve this or do I need to  assign each property individually like so:  IdData.FirstName = MI.FirstName and so on.

       MailingInfo MI = (MailingInfo)HttpContext.Current.Session["MailingInfo"];
                using (IdentityProofContext dbContext = new IdentityProofContext())
                {
                    MailingInfo IdData = dbContext.MailingInfo.Where(r => r.MailId == mId).FirstOrDefault();
                    if (IdData != null)
                    {
                        IdData.Compeletd = "Y";
                        IdData.FirstName = MI.FirstName;
                        IdData.MiddleName= MI.MiddleName;
                        IdData.LastName = MI.LastName;
                        IdData.StreetAddress = MI.StreetAddress;
                        IdData.City = MI.City;
                        IdData.State = MI.State;
                        IdData.Zip = MI.Zip;
                        IdData.PhoneNumber = MI.PhoneNumber;
    
                        dbContext.SaveChanges();

    any help will be highly appreciated.

    Thursday, December 31, 2020 3:35 AM

Answers

  • User351619809 posted

    This is what I did to fix this:

                    dbContext.Attach(MI);
                        MI.Compeletd = "Y";
                        MI.MailId = mId;
                        dbContext.Entry(MI).State = EntityState.Modified;
                        dbContext.SaveChanges();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 31, 2020 7:53 PM

All replies

  • User1120430333 posted

    That didn't work so I tried this way and it worked. I was wondering if their is any simple way to achieve this or do I need to  assign each property individually like so:  IdData.FirstName = MI.FirstName and so on.

    What you have done is how it is suppose to be done, plain and simply put.

    Thursday, December 31, 2020 8:19 AM
  • User-939850651 posted

    Hi anjaliagarwal5,

    According to your description and the code you provided, do you want to copy the attribute values of the class object to another object?

    If this is the case, you could try to use some utils like MiscUtil in this case.

    You could even delete old entity then add a new entity again. Of course, this will reduce work efficiency. 

    Best regards,

    Xudong Peng

    Thursday, December 31, 2020 9:56 AM
  • User351619809 posted

    This is what I did to fix this:

                    dbContext.Attach(MI);
                        MI.Compeletd = "Y";
                        MI.MailId = mId;
                        dbContext.Entry(MI).State = EntityState.Modified;
                        dbContext.SaveChanges();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 31, 2020 7:53 PM