ASP.NET 4.0 and the Entity Framework 4 - Part 3 - Execute Stored Procedures Using the Entity Framework 4
page 6 of 9
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37676/ 110

Step 5: Adding and Updating Records

The next step will be to add the code to allow the user to create new 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.

using (OrderDBContainer db = new OrderDBContainer())
{
  UserAccount userAccount = new UserAccount();
  userAccount.FirstName = txtFirstName.Text;
  userAccount.LastName = txtLastName.Text;
  userAccount.AuditFields.UpdateDate = DateTime.Now;
 
  if (ddlUsers.SelectedItem.Value == "")
  {
    //Adding                    
    userAccount.AuditFields.InsertDate = DateTime.Now;
    db.UserAccounts.AddObject(userAccount);
  }
  else
  {
    //Updating
    userAccount.Id = Convert.ToInt32(ddlUsers.SelectedValue);
    userAccount.AuditFields.InsertDate = Convert.ToDateTime(lblInserted.Text);
 
    db.UserAccounts.Attach(userAccount);
    db.ObjectStateManager.ChangeObjectState(userAccount, 
                                            System.Data.EntityState.Modified);
  }
 
  db.SaveChanges();
 
  lblInserted.Text = userAccount.AuditFields.InsertDate.ToString();
  lblUpdated.Text = userAccount.AuditFields.UpdateDate.ToString();
 
  //Reload the drop down list
  LoadUserDropDownList();
 
  //Select the one the user just saved.
  ddlUsers.Items.FindByValue(userAccount.Id.ToString()).Selected = true;
}

 

This code starts by instanciating the OrderDBContainer object and then creates a new instance of a UserAccount object.  The FirstName and LastName are set to the value entered by the user.  The UpdateDate is set to the current date\time.  The next line checks if the selected item in the Users drop down list is blank.  A blank value would signify that the user selected "Create New User".  If they are creating a new user then the InsertDate is set to the current date\time and the UserAccount object is added to the list of UserAccount objects associated with the OrderDBContainer.  This doesn't add the record to the database, it simply lets the OrderDBContainer know that this object should be added to the database.

If the user was updating a record rather than adding one then the "else" logic would be followed.  The Id is set to the Id of the selected item in the drop down list.  The InsertDate should not be changed so the value that is displayed in the label for the insert date is used.  Since we don't want to add this record we need to call the Attach method on the UserAccounts object.  Again this tells the OrderDBContainer that this object exists. You then have to tell the OrderDBContainer to update the record associated with this object.  To do that you call the ObjectStateManager.ChangeObjectState method and pass in the object to be updated and the Modified entity state enumeration value.

The db.SaveChanges() method actually executes either the insert or the update stored procedure.  Once the record is added or updated then the labels on the screen are updated to reflect the audit dates and the drop down list is refreshed.  You should be able to run the project now and add a few records.  If you run SQL Profiler you can see that the insert stored procedure being called.

exec [dbo].[UserAccounts_Insert]
 @FirstName=N'Vince',
 @LastName=N'Varallo',
 @AuditFields_InsertDate='2010-03-08 18:14:42.4564241',
 @AuditFields_UpdateDate='2010-03-08 18:14:42.4564241'

View Entire Article

User Comments

Title: Greate Writer   
Name: Alex
Date: 2012-09-04 11:54:02 AM
Comment:
Vince, is a great author. Looking forward from him a new book in asp.net 4.5 enterprise application development with all the latest features. EF, etc
Title: Stored Procedure doesn't return columns   
Name: Oscar
Date: 2011-12-28 10:18:54 AM
Comment:
I'm following your tutorial (very well done and thanks). When I reached the point to add the function import for the Select_all stored procedure I get the answer that the Stored Procedure doesn't return columns (My best translation from spanish). If I execute it from the sms client works fine.
Looked around alredy and didn't find anything. May be you could give a hint on where to look for a solution?
Title: Excellent   
Name: John
Date: 2011-05-05 4:00:03 PM
Comment:
excellent article
Title: Thanks a Ton!   
Name: Vipul
Date: 2011-03-22 12:02:05 AM
Comment:
Very helpful article indeed!
Title: Excellent   
Name: Sathiya
Date: 2011-01-09 6:04:12 AM
Comment:
Varallo,
Great Article and very impressive. Please post more article related to Framework 4.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 8:08:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search