EXERCISE TOPIC▼
C# EXERCISES▼
VISUAL C# EXERCISES▼
- Creating forms (4)
- Coding form events (1)
- Laying out your code (2)
- C# variables (4)
- Enumerations and constants (2)
- Conditions (2)
- Modular code (3)
- Arrays (2)
- Looping (2)
- Files and folders (3)
- Properties in C# (3)
- Using lists (3)
- Validating forms (6)
- Toolbars, menus and status bars (1)
- FileDialogs and StreamReaders (1)
- Debugging and trapping errors (1)
- Introduction to DataGridViews (1)
- DataGridView events (3)
- Complex DataGridViews (2)
- Creating classes (4)
- The form as a class (1)
- Data structures (6)
- Inheritance (5)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (2)
- Advanced LINQ (2)
- Entity Frameworks (1)
- LINQ with Entity Frameworks (4)
- Grouping using LINQ (2)
- LINQ to SQL (2)
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.
You can learn how to do this exercise if you attend one of more of the courses listed below!
Software ==> | Visual C# (79 exercises) |
Version ==> | Any version of C# |
Topic ==> | LINQ with Entity Frameworks (4 exercises) |
Level ==> | Average difficulty |
Courses ==> | Fast track C# / Intermediate C# |
- Go into SQL Server Management Studio;
- Open the SQL file you've just unzipped (you can press CTRL + O to do this); then
- 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:

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!