Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

Doing CRUDy stuff with the Table service (part 3) - Deleting entities & Updating entities

3/12/2011 10:48:52 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

4. Deleting entities

In listing 4 we included some event definitions in the markup to handle deletes. It’s now time to implement those event handlers so that when you click the Delete button in the grid, it will delete the corresponding shirt from the Products table in the Table service.

In the listing that follows you’ll see the code-behind that relates to the Delete button.

Listing 6. Code-behind to delete shirts

The code in listing 6 shows how you can delete a shirt from the Products table when the Delete button is clicked in the grid. The DeleteShirt method is very similar to the code in listing 3 that you used to add a shirt. You instantiate the shirt context , add the shirt to be deleted to the context object’s tracking list , and submit the changes to the Table service. Finally, you rebind the grid to display the updated list.

Finding the Entity to Delete

There are a couple of differences between adding and deleting a shirt. When adding a shirt to the product list, you can simply create a new Product object and add that to the tracking list. If you’re deleting an object, you first need to get a local copy of the object that you wish to delete (as you did at in listing 6) and then add that object to the tracking list (as shown at ).

For the sake of simplicity, you can pass the object to be deleted by refetching the object from the Table service. In production code, you should never refetch an object for deletion because this performs an unnecessary call to the Table service. In this example, however, you can take the ProductId (row key) that was passed as the Delete button’s command argument, and then perform a LINQ query to retrieve that entity from the Table service.

The following code shows the LINQ query we used to retrieve the individual shirt:

from item in shirtContext.Product
where item.PartitionKey=="Shirts" && item.RowKey==rowKey
select item

Looking at the LINQ query, you can see that we’re using the IQueryable Product from the shirtContext object as the data source. Because this query is IQueryable, you can modify the query before it’s sent to the server to restrict the result set to only return those entities that reside in the Shirts partition whose row key matches the passed-in ProductId. Because this query is manipulated before it’s sent to the server, all the filtering is performed by the Table service rather than by the client application.

You’ll now be able to add, list, and delete shirts in the Products table.

5. Updating entities

Finally, you need to update the code-behind to allow editing the grid and saving any changes back to the Products table. The following listing contains the code you need to edit the grid.

Listing 7. Updating entities

After editing the grid with your new values, you need to store the modified data back to the Products table. Listing 7 will be called whenever there’s a change to any data in the grid. The GridView’s RowUpdating event is where you’ll perform that update. The process of updating data is very similar to deleting an entity, as shown earlier.

You retrieve the row that has been edited and extract the product ID displayed in the second cell of the row. As before, you instantiate the product context , and at you retrieve a copy of the edited entity from the Table service using a LINQ query, passing the partition key and the row key. At you replace the current name and description of the shirt with the modified data extracted from the text boxes of the row being edited. You then add the modified entity to the shirtContext’s tracking list via the UpdateObject method, commit the changes back to the Table service using the SaveChanges method, take the grid out of edit mode, and rebind the grid to a fresh copy of the data returned from the Table service.

Other -----------------
- Doing CRUDy stuff with the Table service (part 2) - Adding entities & Listing entities
- Developing with the Table service
- Partitioning data across lots of servers : Partitioning the storage account & Partitioning tables
- Modifying an entity to work with the Table service
- How we’d normally represent entities outside of Azure
- A brief overview of the Table service
- BLOBs : Setting shared access permissions
- Enterprise Service Bus with BizTalk Server and Windows Azure : Distributed and Scalable ESB Architecture
- Enterprise Service Bus with BizTalk Server and Windows Azure : The ESB Toolkit
- Enterprise Service Bus with BizTalk Server and Windows Azure : Integration with BizTalk
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server