0

I have created an entity Appraiser and there are methods to select values, display data etc. Now I want to save the changes made after data is displayed, I have a button named SAVE, which will be used to save changes. I am not able to get how to save the changes of this Entity?

Entity name is Appraiser, and I have created methods like get AppriaserDetails etc in DAL, BL and used them in aspx.cs.

This is my code:

 public void UpdateData(Appraiser appId)
    {

        var Appsave = context.Appraisers.FirstOrDefault(App => App.AppraiserId == appId.AppraiserId);
        Appsave.AppraiserName = appId.AppraiserName;
        Appsave.AppraiserPhones = appId.AppraiserPhones;
        Appsave.AppraiserAppraiserCompanyId = appId.AppraiserAppraiserCompanyId;
        Appsave.Address = appId.Address;
        Appsave.City = appId.City;
        Appsave.ProvinceState = appId.ProvinceState;
        Appsave.Email = appId.Email;
        context.SaveChanges();
    }

1 Answer 1

1

If u want to insert new record, then can use

        MyContext.Appraisers.AddObject(appraiserEntityObject);
        MyContext.SaveChanges();

In case of update

        if (appraiserEntityObject.EntityState == EntityState.Detached)
        {
            // In case of web, we got an existing record back from the browser. That                object is not attached to the context yet.
            MyContext.Appraisers.Attach(appraiserEntityObject);
            MyContext.ObjectStateManager.ChangeObjectState(appraiserEntityObject,  EntityState.Modified);
        }
        MyContext.SaveChanges();

Here MyContext is ur ObjectContext

Sign up to request clarification or add additional context in comments.

5 Comments

Where I will have to write the above code? In the aspx.cs in Buttonsave_click event?
Ideally in ur DAL, and from ur aspx call some BL function, which will then call a DAL function. If u have referenced the ObjectContext in ur web project, then u can also call it from aspx.cs, but you should avoid doing that
I am still not getting it, can you tell me in that method in DAL, will I have to pass some id, or it will be the void function?
In ur aspx.cs, are u creating an entity object??? like new appraiserEntity() and assigning the respective values into the properties, if yes, then pass that object to the BL and DAL functions
What should be the function parameters in the Bl, I am getting an error when I am calling the same by creating GetUpdate method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.