# CSLA.Net: Step-by-Step – Web forms Episode II: The Update strikes back.

Welcome back! See I told you, you needn’t wait another 2 months for the next installment.

Today, we’ll have a look at updating an existing customer and adding a new customer via our web interface. I would like to mention that, these tutorials barely touch the surface of the CSLA.Net framework. To fully grasp the framework, buy Rockford Lhotka’s book Expert VB.Net Business Object, the value you’ll get by reading the book will definitely be worth the cost.

Also, to get a more detailed look at how to do things, be sure to check out the ProjectTracker project that comes with the framework.

Right, with that out of the way, let’s get started. Open the NorthwindWEB project; we created in the last post. Select the detailsview control, in my case it has the very original name of DetailsView1, and click on the little arrow top-right. Tick the _Enable Inserting_ and _Enable Editing_ checkboxes. It should look something like this:

[![clip_image002](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/clip-image002-thumb.jpg "clip_image002")](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/clip-image002.jpg)

You’ll notice that the DetailsView control now has 2 hyperlinks entitled _Edit_ and _New_

[![image](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image-thumb10.png "image")](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image10.png)

You’ll notice that when you click on either of the links, it automatically changes the detailsview to edit mode and the hyperlinks’ text to either Update or Insert, depending on which link you clicked:

[![image_3](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image-3-thumb2.png "image_3")](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image-32.png)

I’m just going to show you the quick and dirty way, managing your application state and object persistence is up to you.

Let’s first look at updating your customer. Open up your code view and add the following code:

Protected Sub dsCustomer\_UpdateObject(ByVal sender As Object, ByVal e As Csla.Web.UpdateObjectArgs) 
Handles dsCustomer.UpdateObject
Csla.Data.DataMapper.Map(e.Values, myCustomer)
myCustomer.Save()
End Sub

Remember we declared our Customer object, earlier. As soon as you click on update your changes will be saved. Now that was easy, wasn’t it?

Now for the Insert. In the global.asax file I’ve added the following in the Session\_Start sub, this is to ensure I can persist the current object throughout the application:

Sub Session\_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim myCustomer As Northwind.Library.Customer = Northwind.Library.Customer.GetCustomer(3)
Session.Add("CustomerObject", myCustomer)
End Sub

Next in the DetailsView’s ItemCommand Sub I’ve added the following:

Protected Sub DetailsView1\_ItemCommand(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles DetailsView1.ItemCommand
If e.CommandName = "New" Then
myCustomer = Customer.NewCustomer
Session("CustomerObject") = myCustomer
End If
End Sub

and then in the datasource’s InsertObject sub add the following:

Protected Sub dsCustomer\_InsertObject(ByVal sender As Object, ByVal e As Csla.Web.InsertObjectArgs) 
Handles dsCustomer.InsertObject
myCustomer = Session("CustomerObject")
Csla.Data.DataMapper.Map(e.Values, myCustomer, "CustomerID")
myCustomer.Save()
End Sub

So what’ll happen is that as soon as you click on the new link. A Customer.NewCustomer Object will be save in your Session’s CustomerObject. The as the page reloads, it shows the Customer object stored in the session. This object is then saved to the database.

I hope this made sense, it certainly is a quick and dirty way of doing it. Check the Project Tracker’s web project to see a proper implementation.

Until next time..cheerio.
