Estimated Time: 60 minutes
Ensure that you have cloned the 20483C directory from GitHub. It contains the code segments for this course's labs and demos. (https://github.com/MicrosoftLearning/20483-Programming-in-C-Sharp/tree/master/Allfiles)
- Navigate to the [Repository Root]\Allfiles\Mod07\Labfiles\Databases folder, and then double-click SetupSchoolGradesDB.cmd.
- Close File Explorer, If a Windows protected your PC dialog appears, click More info and then click Run Anyway.
- Open Visual Studio 2017.
- In Visual Studio, on the File menu, point to Open, and then click Project/Solution.
- In the Open Project dialog box, browse to [Repository Root]\Allfiles\Mod07\Labfiles\Starter\Exercise 1, click GradesPrototype.sln, and then click Open.
Note : If any Security warning dialog box appears, clear Ask me for every project in this solution check box and then click OK.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, in the Installed list, click Visual C#, and then click Class Library(.NET Framework).
- In the Name text box, type Grades.DataModel, and then click OK.
- Right-click the Grades.DataModel project, point to Add and then click New Item
- In the Add New Item – Grades.DataModel dialog box, in the templates list, click ADO.NET Entity Data Model.
- In the Name text box, type GradesModel, and then click Add.
- In the Entity Data Model Wizard, on the Choose Model Contents page, click EF Designer from database, and then click Nextץ
- On the Choose Your Data Connection page, click New Connection.
- If the Choose Data Source dialog box appears, in the Data source list, click Microsoft SQL Server, and then click Continue.
- In the Connection Properties dialog box, in the Server name text box, type (localdb)\MSSQLLocalDB.
- In the Select or enter a database name list, click SchoolGradesDB, and then click OK.
- In the Entity Data Model Wizard, on the Choose Your Data Connection page, click Next.
- On the Choose Your Version page, choose Entity Framework 6.x, and then click Next.
- On the Choose Your Database Objects and Settings page, expand Tables, expand dbo, select the following tables, and then click Finish:
- Grades
- Students
- Subjects
- Teachers
- Users
- If the Security Warning dialog box appears, click Do not show this message again, and then click OK.
- On the Build menu, click Build Solution.
- In the Designer window, review the entities that have been generated.
- Review the properties and navigation properties of the Grade entity.
- Right-click the heading of the Grades entity, and then click Table Mapping.
- In the Mapping Details – Grades pane, review the mappings between the columns in the database table and the properties of the entity.
- In Solution Explorer, expand GradesModel.edmx, expand GradesModel.Context.tt, and then double-click GradesModel.Context.cs.
- In the Code window, note that the wizard has created a DbContext object named SchoolGradesDBEntities.
- In Solution Explorer, expand GradesModel.tt, and then double-click Grades.cs.
- Note that the wizard has created one property for each column in the Grades database table.
- On the File menu, click Save All.
- On the File menu, click Close Solution.
Result: After completing this exercise, the prototype application should include an Entity Data Manager (EDM) that you can use to access the The School of Fine Arts database.
- Open Visual Studio 2017.
- In Visual Studio, on the File menu, point to Open, and then click Project/Solution.
- In the Open Project dialog box, browse to [Repository Root]\Allfiles\Mod07\Labfiles\Starter\Exercise 2, click GradesPrototype.sln, and then click Open.
Note : If any Security warning dialog box appears, clear Ask me for every project in this solution check box and then click OK.
- In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.
- On the View menu, click Task List.
- In the Task List window, in the Categories drop-down box, choose Entire Solution.
- Double-click the TODO: Exercise 2: Task 1a: Find all the grades for the student. task.
- In the code editor, click in the blank line below the comment, and then type the following code:
List<Grades.DataModel.Grade> grades = new List<Grades.DataModel.Grade>(); foreach (Grades.DataModel.Grade grade in SessionContext.DBContext.Grades) { if (grade.StudentUserId == SessionContext.CurrentStudent.UserId) { grades.Add(grade); } }
- In the Task List window, double-click the TODO: Exercise 2: Task 1b: Display the grades in the studentGrades ItemsControl by using databinding. task.
- In the code editor, click in the blank line below the comment, and then type the following code:
studentGrades.ItemsSource = grades;
- On the Build menu, click Build Solution.
- On the Debug menu, click Start Without Debugging.
- When the application loads, in the Username text box, type vallee, and in the Password text box, type password99, and then click Log on.
- In the Class 3C view, click Kevin Liu.
- Verify that Kevin Liu’s grades are listed.
- Note that the subject column uses the subject ID rather than the subject name, and then close the application.
- In Visual Studio, in the Task List window, double-click the TODO: Exercise 2: Task 2a: Convert the subject ID provided in the value parameter task.
- In the code editor, click in the blank line below the comment, and then type the following code:
int subjectId = (int)value; var subject = SessionContext.DBContext.Subjects.FirstOrDefault(s => s.Id == subjectId);
- In the Task List window, double-click the TODO: Exercise 2: Task 2b: Return the subject name or the string “N/A” task.
- In the code editor, delete the following line of code:
return value;
- In the code editor, click in the blank line below the comment, and then type the following code:
return subject.Name != string.Empty ? subject.Name : "N/A";
- Save the file.
- In the Task List window, double-click the TODO: Exercise 2: Task 3a: Use the GradeDialog to get the details of the new grade task.
- In the code editor, click in the blank line below the comment, and then type the following code:
GradeDialog gd = new GradeDialog();
- In the Task List window, double-click the TODO: Exercise 2: Task 3b: Display the form and get the details of the new grade task.
- In the code editor, click in the blank line below the comment, and then type the following code:
if (gd.ShowDialog().Value) {
- Click in the blank line below the final TODO comment in this try block, and then type the following code:
} - In the Task List window, double-click the TODO: Exercise 2: Task 3c: When the user closes the form, retrieve the details of the assessment grade from the form and use them to create a new Grade object task.
- In the code editor, click in the blank line below the comment, and then type the following code:
Grades.DataModel.Grade newGrade = new Grades.DataModel.Grade(); newGrade.AssessmentDate = gd.assessmentDate.SelectedDate.Value; newGrade.SubjectId = gd.subject.SelectedIndex; newGrade.Assessment = gd.assessmentGrade.Text; newGrade.Comments = gd.comments.Text; newGrade.StudentUserId = SessionContext.CurrentStudent.UserId;
- In the Task List window, double-click the TODO: Exercise 2: Task 3d: Save the grade task.
- In the code editor, click in the blank line below the comment, and then type the following code:
SessionContext.DBContext.Grades.Add(newGrade); SessionContext.Save();
- In the Task List window, double-click the TODO: Exercise 2: Task 3e: Refresh the display so that the new grade appears task.
- In the code editor, click at the end of the comment, press Enter, and then type the following code:
Refresh();
- On the Build menu, click Build Solution.
- On the Debug menu, click Start Without Debugging.
- When the application loads, in the Username text box, type vallee, and in the Password text box, type password99, and then click Log on.
- In the Class 3C view, click Kevin Liu.
- Verify that the list of grades now displays the subject name, not the subject ID.
- In the Report Card view, click Add Grade.
- In the New Grade Details dialog box, in the Subject list, click Geography, in the Assessment text box, type A+, in the Comments text box, type Well done!, and then click OK.
- Verify that the new grade is added to the list, and then close the application.
- In Visual Studio, on the File menu, click Close Solution.
Result: After completing this exercise, users will see the grades for the current student and add new grades.
- Open Visual Studio 2017.
- In Visual Studio, on the File menu, point to Open, and then click Project/Solution.
- In the Open Project dialog box, browse to [Repository Root]\Allfiles\Mod07\Labfiles\Starter\Exercise 3, click GradesPrototype.sln, and then click Open.
Note : If any Security warning dialog box appears, clear Ask me for every project in this solution check box and then click OK.
- In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.
- In Solution Explorer, right-click Grades.DataModel project, point to Add, and then click Class.
- In the Add New Item – Grades.DataModel dialog box, in the Name text box, type customTeacher.cs, and then click Add.
- In the code editor, modify the class declaration as shown in the following code:
public partial class Teacher
- In the code editor, in the Teacher class, type the following code:
private const int MAX_CLASS_SIZE = 8;
- In the code editor, in the Teacher class, type the following code:
public void EnrollInClass(Student student) { // Verify that this teacher's class is not already full. // Determine how many students are currently in the class. int numStudents = (from s in Students where s.TeacherUserId == UserId select s).Count(); // If the class is already full, another student cannot be enrolled. if (numStudents >= MAX_CLASS_SIZE) { // So throw a ClassFullException and specify the class that is full. throw new ClassFullException("Class full: Unable to enroll student", Class); } // Verify that the student is not already enrolled in another class. if (student.TeacherUserId == null) { // Set the TeacherID property of the student. student.TeacherUserId = UserId; } else { // If the student is already assigned to a class, throw an ArgumentException. throw new ArgumentException("Student", "Student is already assigned to a class"); } }
- In the Task List window, double-click the TODO: Exercise 3: Task 1a: Call the EnrollInClass method to assign the student to this teacher’s class task.
- In the code editor, click in the blank line below the comment, and then type the following code:
SessionContext.CurrentTeacher.EnrollInClass(student);
- In the Task List window, double-click the TODO: Exercise 3: Task 1b: Save the updated student/class information back to the database task.
- In the code editor, click in the blank line below the comment, and then type the following code:
SessionContext.Save();
- In Solution Explorer, right-click Grades.DataModel, point to Add, and then click Class.
- In the Add New Item – Grades.DataModel dialog box, in the Name text box, type customGrade.cs, and then click Add.
- In the code editor, modify the class declaration as shown in the following code:
public partial class Grade
- In the code editor, in the Grade class, type the following code:
public bool ValidateAssessmentDate(DateTime assessmentDate) { // Verify that the user has provided a valid date. // Check that the date is no later than the current date. if (assessmentDate > DateTime.Now) { // Throw an ArgumentOutOfRangeException if the date is after the current date. throw new ArgumentOutOfRangeException("Assessment Date", "Assessment date must be on or before the current date"); } else { return true; } }
- In the code editor, below the existing using directives, type the following code:
using System.Text.RegularExpressions;
- In the code editor, in the Grade class, type the following code:
public bool ValidateAssessmentGrade(string assessment) { // Verify that the grade is in the range A+ to E-. // Use a regular expression: A single character in the range A-E at the start of the string followed by an optional + or - at the end of the string. Match matchGrade = Regex.Match(assessment, @"^[A-E][+-]?$"); if (!matchGrade.Success) { // If the grade is not valid, throw an ArgumentOutOfRangeException. throw new ArgumentOutOfRangeException("Assessment", "Assessment grade must be in the range A+ to E-"); } else { return true; } }
- In the Task List window, double-click the TODO: Exercise 3: Task 2a: Create a Grade object. task.
- In the code editor, click in the blank line below the comment, and then type the following code:
Grades.DataModel.Grade testGrade = new Grades.DataModel.Grade();
- In the Task List window, double-click the TODO: Exercise 3: Task 2b: Call the ValidateAssessmentDate method task.
- In the code editor, click in the blank line below the comment, and then type the following code:
testGrade.ValidateAssessmentDate(assessmentDate.SelectedDate.Value);
- In the Task List window, double-click the TODO: Exercise 3: Task 2c: Call the ValidateAssessmentGrade task.
- In the code editor, click in the blank line below the comment, and then type the following code:
testGrade.ValidateAssessmentGrade(assessmentGrade.Text);
- On the Build menu, click Build Solution.
- On the Debug menu, click Start Without Debugging.
- When the application loads, in the Username text box, type vallee, and in the Password text box, type password99, and then click Log on.
- When the application loads, click Enroll Student.
- In the Assign Student dialog box, click Eric Gruber, in the Confirm message box, click Yes, and then in the Error enrolling student message box, click OK.
- In the Unassign Student dialog box, click Close.
- In the Class 3C view, click Kevin Liu, and then click Add Grade.
- In the New Grade Details dialog box, in the Date text box, type tomorrow’s date, and then click OK.
- In the Error creating assessment message box, click OK.
- In the New Grade Details dialog box, in the Date text box, type 8/19/2012, in the Assessment text box, type F+, and then click OK.
- In the Error creating assessment message box, click OK.
- In the New Grade Details dialog box, in the Assessment text box, type A+, in the Comments text box, type Well done!, and then click OK.
- Verify that the new grade is added to the list, and then close the application.
- In Visual Studio, on the File menu, click Close Solution.
Result: After completing this exercise, the application will raise and handle exceptions when invalid data is entered.
©2018 Microsoft Corporation. All rights reserved.
The text in this document is available under the Creative Commons Attribution 3.0 License, additional terms may apply. All other content contained in this document (including, without limitation, trademarks, logos, images, etc.) are not included within the Creative Commons license grant. This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes.
This document is provided "as-is." Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. You bear the risk of using it. Some examples are for illustration only and are fictitious. No real association is intended or inferred. Microsoft makes no warranties, express or implied, with respect to the information provided here.