Visual C# | LINQ with Entity Frameworks exercise | Add Guatemalan volcano event to events table

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the course listed below!

Software ==> Visual C#  (79 exercises)
Version ==> Any version of C#
Topic ==> LINQ with Entity Frameworks  (4 exercises)
Level ==> Average difficulty
Course ==> Intermediate C#
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help). Once you've done this:
  1. Go into SQL Server Management Studio;
  2. Open the SQL file you've just unzipped (you can press CTRL + O to do this); then
  3. Execute this script.

This will generate the database that you'll need to use in order to do this exercise (note that the database and script are only to be used for exercises published on this website, and may not be reused or distributed in any form without the prior written permission of Wise Owl).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

On June 3rd 2018 the Guatemalan volcano Fuego erupted - this event needs to be added to the tblEvent table (but first Guatemala needs to be added to the tblCountry table).

If you haven't already got one, create a Windows Forms project (using Visual C#, obviously) called wiseowl.  Also if you haven't already done so, first right-click in Solution Explorer and add an ADO.NET Entity Data Model based on the WorldEvents database (tick all of the tables). 

Create a new form called frmVolcano with a single clickable button.  When you click on this button your code should:

Task Notes
Add a country Guatemala isn't in the tblCountry table, so create a new tblCountry object, set its CountryName property, add it to your data context's collection of countries and then save changes for the data context!
Add an event Similarly add the eruption event into the tblEvent table.  Use category id number 4 (this corresponds to Natural World events).  You'll need to set the CountryId, CategoryId, EventDate, EventName  and EventDetails properties.

Before displaying details of the event you've just added, you need to refresh the data context.  One fairly primitive way to do this is to dispose of it and then recreate it:

// save these changes

dbEvents.SaveChanges();

// make sure have latest changes

dbEvents.Dispose();

dbEvents = new WorldEventsEntities();

Now use LINQ to get the event whose id number equals the id number of the event you've just added, then display these details in a message box:

Event details

Your message could look something like this.

 

Finally, add code to remove the event with the id number that you've just added, using code something like this:

// now remove this event

dbEvents.tblEvents.Remove(newEvent);

dbEvents.SaveChanges();

We'll leave Guatemala in the countries table for posterity!

This page has 0 threads Add post