03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

588 x CHAPTER 15 WORKING WITH DATA — ADVANCED TOPICS<br />

The code checks to make sure review is not Noth<strong>in</strong>g/null before it tries to access its properties.<br />

The chances of the review be<strong>in</strong>g null <strong>in</strong> this example are pretty small because you access the<br />

Add/Edit page by click<strong>in</strong>g an exist<strong>in</strong>g item <strong>in</strong> the Reviews page, so you can be pretty sure the item<br />

is there. However, this is not always the case, especially not <strong>in</strong> public-fac<strong>in</strong>g pages. Your clients<br />

may have a bookmark for a page with a specific ID <strong>in</strong> the query str<strong>in</strong>g. If you then delete that item<br />

from the database <strong>and</strong> your users access the page us<strong>in</strong>g the old bookmark, the review can’t be<br />

found <strong>and</strong> a so-called Null Reference exception occurs.<br />

The same defensive cod<strong>in</strong>g mechanism is used to preselect the genre <strong>in</strong> the drop-down list. In<br />

this case you can be sure the Genre still exists <strong>in</strong> the database because there’s a relationship<br />

between the Id column of the Genre table <strong>and</strong> the GenreId of the Review table. However, check<strong>in</strong>g<br />

to make sure an item exists <strong>in</strong> a DropDownList control before you try to select it is a best<br />

practice <strong>and</strong> helps <strong>in</strong> avoid<strong>in</strong>g other Null Reference exceptions. Because the DropDownList with<br />

genres hasn’t been populated at this stage, you need to call DataB<strong>in</strong>d() first. This forces the<br />

EntityDataSource control to get the genres <strong>and</strong> add them to the DropDownList. Afterward, the<br />

code can successfully f<strong>in</strong>d <strong>and</strong> preselect the appropriate item.<br />

F<strong>in</strong>ally, when you click the Save button for an edited item, the exact same code is fired that was<br />

used to <strong>in</strong>sert a new item.<br />

If you were us<strong>in</strong>g validation controls (<strong>and</strong> you really should, as you learned <strong>in</strong> Chapter 9),<br />

you need to check whether or not the page is valid before you proceed with sav<strong>in</strong>g the Review<br />

<strong>in</strong>stance:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Protected Sub SaveButton_Click(sender As Object, e As EventArgs) _<br />

H<strong>and</strong>les SaveButton.Click<br />

If (Page.IsValid) Then<br />

Us<strong>in</strong>g myEntities As New PlanetWroxEntities()<br />

C#<br />

protected void SaveButton_Click(object sender, EventArgs e)<br />

{<br />

if (Page.IsValid)<br />

{<br />

us<strong>in</strong>g (PlanetWroxEntities myEntities = new PlanetWroxEntities())<br />

This is really all there is to add<strong>in</strong>g <strong>and</strong> edit<strong>in</strong>g new reviews us<strong>in</strong>g your own code aga<strong>in</strong>st the Entities<br />

Framework. I realize it may look a little funky at first because you need to reset your head, forget<br />

about smart controls <strong>and</strong> their many properties <strong>and</strong> events, <strong>and</strong> th<strong>in</strong>k <strong>in</strong> straight code. However, EF<br />

makes this pretty straightforward, <strong>and</strong> most of it comes down to query<strong>in</strong>g entities, copy<strong>in</strong>g values<br />

from or to an object’s properties, <strong>and</strong> call<strong>in</strong>g SaveChanges to propagate the changes back to the<br />

database.<br />

Clearly, this is just the beg<strong>in</strong>n<strong>in</strong>g. There’s a whole lot more you can do once you start writ<strong>in</strong>g your own<br />

code, whether or not it targets EF. For more <strong>in</strong>formation, get yourself a copy of Programm<strong>in</strong>g Entity<br />

Framework Second Edition by Julia Lerman or Professional <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> <strong>in</strong> C# <strong>and</strong> <strong>VB</strong> from Wrox<br />

(ISBN: 978-1-118-31182-0).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!