2. Adding entities
Now that you have your
context class, you can start creating your product-management web page.
To do this, you need to add a new ASP.NET web page called products.aspx.
At this stage, we won’t
generate the grid listing all the Hawaiian shirts for sale; we’ll only
write the code required to add shirts to the Products table. Therefore,
you only need to add the markup for the bottom section of the products
page.
The listing that follows
contains the ASPX code that you should add to the products.aspx page.
Listing 2. Adding the shirt section of the products.aspx page
When the Add button is clicked
in listing 2, the shirt entity will be added to the
Products table.
Listing 3
contains the code for the Add button’s click event. This code should be
added to the products.aspx code-behind.
Listing 3. Add the entity to the Products table
To add the new shirt
details, which were entered on the web page, to the Products table, you
need to extract all the information entered about the product (ID, name,
and description) and create a new instance of the Product
class called newShirt . Once
you’ve created an instance of the shirt , you
add the shirt entity to a tracking list held in the context object for the product list.
When you add the entity to
the tracking list , you
also specify the table that the entity should be added to. The product
context object (shirtContext)
maintains a list of all objects that you have changed as part of this
operation. You can create, update, or delete objects from the product
list, and you can add all these changes locally to the tracking list.
Eventually, when you wish to
perform all the changes on the server side, you can invoke the SaveChanges method on the context object , which
will apply all the tracked changes on the server side using ADO.NET Data
Services via the REST API.
Was the Entity Added?
You should now be able to run
the product-management web page and add new shirts to the product list.
We haven’t yet implemented the grid to display the list of entities in
the product list—we’ll do that in the next section. In the meantime you
can always check that your entity exists by querying the development
storage database using the following statement in SQL Management Studio:
3. Listing
entities
It’s
now time to extend the products.aspx web page to display the list of
shirts stored in the Products table. The following listing contains the
code required for your grid. This code should be placed above the code
in listing 2.
Listing 4. ASP.NET grid for displaying shirts
The grid in listing 4
will display the product ID, name, and description for each shirt in
the Products table. The name and description columns will both be
editable, but the product ID won’t be.
At this stage, you’ve defined
the markup required to edit and delete the shirts in the table, but we
won’t write the code-behind for these events just yet. The following
listing contains the code-behind for the products.aspx page, which
you’ll require to populate the new grid with the list of shirts.
Listing 5. Populating the list of shirts
As you can see, the code to
bind the GridView to the list of
shirts held in the Products table is pretty simple. On the first load of
the web page , the BindGrid is
called to populate the grid with the list of shirts retrieved from the
Products table. method
To retrieve the list of
shirts, you instantiate the product context object (shirtContext)
and
set the data source of the grid to the Product property of the context object.
By returning an IQueryable
list of products from the context object, you can define a query using
LINQ that will be executed on the server side when you enumerate the
list of objects, which happens when the grid is data bound .
To keep this example simple,
we won’t perform any server-side filtering at this stage. We’ll simply
return a list of all shirts in the Products table as shown at .