03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

666 x CHAPTER 17 PERSONALIZING WEBSITES<br />

pictures from the database first, or manually enter a username for each exist<strong>in</strong>g row.) Save your<br />

changes to the table <strong>and</strong> close SSMS.<br />

2. Open the ADO.<strong>NET</strong> Entity Data Model file PlanetWrox.edmx from the App_Code folder, rightclick<br />

an empty space <strong>in</strong> the designer, <strong>and</strong> choose<br />

Update Model from Database. Wait until VS has analyzed<br />

your database <strong>and</strong> click F<strong>in</strong>ish. The UserName<br />

column <strong>in</strong> the database now shows up as a property of<br />

the PhotoAlbum class (see Figure 17-10).<br />

Save your changes <strong>and</strong> close the file.<br />

3. Open the Web.config file, <strong>and</strong> below the exist<strong>in</strong>g<br />

elements, add the follow<strong>in</strong>g two <br />

elements to block access to the two referenced<br />

files for anonymous users:<br />

FIGURE 17-10<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Save your changes <strong>and</strong> close the Web.config file.<br />

4. Open NewPhotoAlbum.aspx <strong>in</strong> Design View, locate the EntityDataSource control, <strong>and</strong> set up an<br />

event h<strong>and</strong>ler for its Insert<strong>in</strong>g event us<strong>in</strong>g the Events tab of the Properties Grid. Add the follow<strong>in</strong>g<br />

code to the h<strong>and</strong>ler that VS created for you:<br />

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

Protected Sub EntityDataSource1_Insert<strong>in</strong>g(sender As Object,<br />

e As EntityDataSourceChang<strong>in</strong>gEventArgs) H<strong>and</strong>les EntityDataSource1.Insert<strong>in</strong>g<br />

Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)<br />

myPhotoAlbum.UserName = User.Identity.Name<br />

End Sub<br />

C#<br />

protected void EntityDataSource1_Insert<strong>in</strong>g(object sender,<br />

EntityDataSourceChang<strong>in</strong>gEventArgs e)<br />

{<br />

PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;<br />

myPhotoAlbum.UserName = User.Identity.Name;<br />

}

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

Saved successfully!

Ooh no, something went wrong!