ASP.NET 4.0 and the Entity Framework 4 - Part 4 - A 3 Layered Approach to the Entity Framework
page 7 of 10
by Vince Varallo
Average Rating: 
Views (Total / Last 10 Days): 59152/ 93

Step 6: Adding and Updating Records

The next step will be to add the code to allow the user to create or update records in the table.

1.    Switch to Design view and double click on the Save button to create the button click event handler.

2.    Add the following code to the click event handler.

protected void btnSave_Click(object sender, EventArgs e)
    ArrayList validationErrors = new ArrayList();
    UserAccountEO userAccount = new UserAccountEO();
    if (ddlUsers.SelectedItem.Value != "")
        userAccount.Id = Convert.ToInt32(ddlUsers.SelectedValue);
    userAccount.FirstName = txtFirstName.Text;
    userAccount.LastName = txtLastName.Text;
    userAccount.Save(ref validationErrors);
    if (validationErrors.Count > 0)
        //Reload the drop down list
        //Select the one the user just saved.
        ddlUsers.Items.FindByValue(userAccount.Id.ToString()).Selected = true;
        lblErrorLabel.Visible = false;
        lblErrorMessages.Visible = false;


private void ShowValidationMessages(ArrayList validationErrors)
    if (validationErrors.Count > 0)
        lblErrorLabel.Visible = true;
        lblErrorMessages.Visible = true;
        lblErrorMessages.Text = "<br />";
        foreach (string message in validationErrors)
            lblErrorMessages.Text += message + "<br />";
        lblErrorLabel.Visible = false;
        lblErrorMessages.Visible = false;

This code creates an ArrayList to hold any validation errors that are discovered in the Business Layer.  It then creates an instance of the UserAccountEO object and sets its properties based on what the user has entered on the web page.  After the Save method is called the Count of the validationErrors is checked.  If there are errors then they are displayed to the user by calling the ShowValidationMessages method.

View Entire Article

User Comments

Title: Where is the UserAccounts table diagram?   
Name: Fernando Linhares
Date: 2012-07-11 6:12:05 PM
Thanks for the article series.
Where is the UserAccounts table diagram picture?
Title: without stored procedures   
Name: Junior
Date: 2012-06-06 2:21:42 PM
how would this approach without stored procedures ?
Title: problem   
Name: zia
Date: 2012-05-22 9:33:03 AM
Brother i cant see the pictures u given in the project
Title: Mr   
Name: Varun Maggo
Date: 2011-03-16 4:16:28 AM
You rockmaan!
Title: Nice work!   
Name: janmeis
Date: 2011-03-02 11:52:40 AM
Thanks Vince, I've created the project according the article and it works fine! There's one minor bug, the SCOPEIDENTITY returned by the UserAccounts_Insert procedure is mapped as decimal not int, so in the item 16 the procedure must be as follows " need to change the "Returns a Collection Of" to Scalars and choose decimal from the list." Then it can be casted to int in the "public static int Insert". Link with the uploaded project is attached.
Title: connectionstring problem   
Name: Mark
Date: 2010-12-25 7:59:09 AM
I am getting server error on data source keyword.

Keyword not supported: 'data source'.

Exception Details: System.ArgumentException: Keyword not supported: 'data source'.
Title: Lnking to a data Object   
Name: Richard
Date: 2010-10-01 4:12:41 PM
So far so good, but now I have this BLL how do I use it say in a datagrid?
Title: Where is the concurrency management???   
Name: Matt
Date: 2010-07-30 12:52:28 PM
Where is the concurrency management code? If you load a user account from the database and five minutes later the person clicks the submit button how do you handle the issue where someone else already updated that record and you need to check for that???
Title: Why add the DAL and BLL as separate projects?   
Name: Alan
Date: 2010-07-12 12:16:59 PM
Even assuming that this is a good way to structure your application (which seems to be a matter of debate judging by other people's comments), why did you do the DAL and BLL as separate projects? I don't see any benefit at all to this.

Given that this is a web site, why not just add the .cs files for the DAL and BLL to the App_Code folder? You would then have the objects you need to separate the layers, but you wouldn't have the extra mess of separate projects and DLLs.
Title: Not convincing   
Name: Kum
Date: 2010-05-09 11:27:07 AM
Duplicate data model, one generated by entity framework in DL and another hand written in BL...
There should be a better way.
Title: Is it realy the way?   
Name: Kristian
Date: 2010-05-07 6:54:23 AM
Though I loved article part 1 and 2, this seems like a bad approach, like Mant wrote. Why do another mapping?
Title: Good Work   
Name: Rajbharat
Date: 2010-05-07 6:40:38 AM
Its good and simple to follow..thanks for ur good work
Title: Appreciate that!   
Name: Waldek
Date: 2010-05-06 5:57:10 AM
Thanks a lot Vince! Based on your articles all kinds of web.CRUD applications can now be built with a little hassle!
Title: use IsNullOrWhiteSpace instead if .Trim() == ""   
Name: Binoj Antony
Date: 2010-05-06 5:14:00 AM
Suggest that you improvise FirstName.Trim() == "" with

Also the most of the else blocks that does just a return false , here you could eliminate the else and just write return false; (This last one is just an opinion)
Title: Bad Approach   
Name: Mant
Date: 2010-05-05 11:05:10 AM
It seems what you have done is take the approach used in 1.1/2.0 with datasets then just swapped them with the Entity Framework completely missing the point of an ORM. I would suggest reading up on the Repository pattern, Unit of Work pattern, specification pattern and persistence ignorance. There are some great examples of using the framework out there, but just about everything in this one is a poor way of doing things.
Title: Great article!   
Name: Rich D.
Date: 2010-05-05 9:13:07 AM
I loved the article. I was looking for some direction and ideas on how to create a BL. But, I thought the idea was to save much of the template hand coding by using t4 templates to generate POCO classes for the BL. The template for self-tracking entities looks promising for n-tier architectures.
Title: ASP.NET 4.0 and the Entity Framework 4 - Part 4 - A 3 Layered Approach to the Entity Framework   
Name: Ray
Date: 2010-05-05 3:16:16 AM
Thanks Vince for a very helpful article.......
Title: ASP.NET 4.0 and the Entity Framework 4 - Part 4 - A 3 Layered Approach to the Entity Framework   
Name: tvs
Date: 2010-05-04 9:35:39 PM
a very good article... well done vince...

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-23 1:12:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search